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();
}
}
推荐阅读
- Integer常量池结合源码解析
- Java基础|Java 打印空心等腰三角形(方法2)
- gradle 每次运行都会下载依赖的解决办法
- 使用vector代替数组
- 如何获取ResultSet的行数和列数
- Java 时间戳格式化
- Java基础|Android开发——JVM、Dalvik以及ART的区别
- XML|XML报文转Map
- Java 8 时间,字符串和Long时间戳互转
- JAVA基础|JAVA基础(TreeMap键是Student值是String案例)