三个线程循环输出ABC10次

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ABC {

private static int state=0;

/**
* @param args
*/
public static void main(String[] args) {
final Lock lock=new ReentrantLock();

Thread A=new Thread(new Runnable(){
public void run(){

while(state<=30){
lock.lock();
if(state%3==0){
System.out.println("A");
state ++;
}
lock.unlock();
}
}
});

Thread B=new Thread(new Runnable(){
public void run(){

while(state<=30){
lock.lock();
if(state%3==1){
System.out.println("B");
state ++;
}
lock.unlock();
}
}
});

Thread C=new Thread(new Runnable(){
public void run(){

while(state<=30){
lock.lock();
if(state%3==2){
System.out.println("C");
state ++;
}
lock.unlock();
}
}
});

A.start();
B.start();
C.start();

}

}

你可能感兴趣的:(java基础)