两种实现方式1:普通synchronized版实现、 2:使用Semaphore版实现
一:普通synchronized版实现
在这里插入代码片public class Ceshi999 {
static int result = 0;
public static void main(String[] args){
Thread thread = new Thread(new Jiou(0));
thread.start();
Thread thread1 = new Thread(new Jiou(1));
thread1.start();
}
static class Jiou implements Runnable{
private int i;
public Jiou(int i){
this.i = i;
}
@Override
public void run() {
while (result<100){
synchronized (Jiou.class){
System.out.println("thread"+i+":"+result++);
Jiou.class.notify();
try {
Jiou.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
二:使用Semaphore版实现
public class Ceshi888 {
static int result = 0;
public static void main(String[] args) throws InterruptedException {
int N = 3;
Thread threads[] = new Thread[N];//声明N个数据
Semaphore semaphore[] = new Semaphore[N];//声明N个信息号量
for(int i=0;i<N;i++){
semaphore[i] = new Semaphore(1);
if(i!=N-1){//最后一个不获取信号量许可
semaphore[i].acquire();//获取一个信号量许可
}
}
for(int i=0;i<N;i++){
Semaphore lastSemaphore = i==0 ? semaphore[N-1]:semaphore[i-1];//0:2 1:0 2:1 0的最后一个信号量是2,1的最后一个信号量是0,2的后一个信号量是1
Semaphore currentSemphore = semaphore[i];//当前信号量
int index = i;
threads[i] = new Thread(new Runnable() {
@Override
public void run() {
while(true){
try {
lastSemaphore.acquire();//每次只会有一个信号量获得许可,顺序为2,0,1
System.out.println("thread"+index+":"+( result++));
if(result>100){
System.exit(0);
}
}catch (Exception e){
e.printStackTrace();
}finally {
currentSemphore.release();
}
}
}
});
threads[i].start();
}
}
}