当前位置:编程学习 > JAVA >>

多线程经典入门

package com.xml.test;

public class MyThread {

 /**
  * @param args
  */ www.zzzyk.com
 int j;
 public synchronized void inc(){
  j++;
  System.out.println(Thread.currentThread().getName()+"--inc"+j);
 }
 public synchronized void des(){
  j--;
  System.out.println(Thread.currentThread().getName()+"--des"+j);
 }
 class InnerClass_a implements Runnable{
  public void run() {
   for(int i=0;i<100;i++){
    inc();
   }
  }
 }
 class InnerClass_b implements Runnable{
  public void run() {
   for(int i=0;i<100;i++){
    des();
   }
  }
 }
 public static void main(String[] args) {
 
  MyThread mythread = new MyThread();
  InnerClass_a class_a = mythread.new InnerClass_a();
  InnerClass_b class_b = mythread.new InnerClass_b();
  for(int i=0;i<1;i++){
   Thread thread_a = new Thread(class_a);
   thread_a.start();
   Thread thread_b = new Thread(class_b);
   thread_b.start();
  }
 }

}

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,