import java.util.concurrent.Semaphore;
public class test {
private static Semaphore s1 = new Semaphore(1);
private static Semaphore s2 = new Semaphore(0);
private static Semaphore s3 = new Semaphore(0);
private static int num = 6;
static Thread t1 = new Thread(new Runnable() {
private int sum = 0;
public void run() {
while (sum < num) {
try {
s1.acquire();
System.out.print("A");
sum ++;
} catch (Exception e) {
e.printStackTrace();
}finally {
s2.release();
}
}
}
});
static Thread t2 = new Thread(new Runnable() {
private int sum = 0;
public void run() {
while (sum < num) {
try {
s2.acquire();
System.out.print("B");
sum ++ ;
} catch (Exception e) {
e.printStackTrace();
}finally {
s3.release();
}
}
}
});
static Thread t3 = new Thread(new Runnable() {
private int sum = 0;
public void run() {
while (sum < num) {
try {
s3.acquire();
System.out.println("C");
sum++;
} catch (Exception e) {
e.printStackTrace();
}finally {
s1.release();
}
}
}
});
}
指定多个线程按顺序循环执行
猜你喜欢
转载自blog.csdn.net/yongqi_wang/article/details/89006063
今日推荐
周排行