1、三个线程按照顺序打印1-100
每个线程各自打印1、2、3的倍数
然后用锁和wait来控制线程的顺序
使用匿名内部类创建线程
public class Test2 {public static final Object lock = new Object();public static int number=1;public static void main(String[] args) {//这个线程打印的是1的倍数Thread ThreadA =new Thread(new Runnable() {@Overridepublic void run() {synchronized(lock){try {while(number<=100){if(number%3==1){System.out.println("ThreadA:" + number);number++;lock.notifyAll();}else{lock.wait();}}} catch (InterruptedException e) {e.printStackTrace();}}}});Thread ThreadB =new Thread(new Runnable() {@Overridepublic void run() {synchronized(lock){try {while(number<=100){if(number%3==2){System.out.println("ThreadB:" + number);number++;lock.notifyAll();}else{lock.wait();}}} catch (InterruptedException e) {e.printStackTrace();}}}});Thread ThreadC =new Thread(new Runnable() {@Overridepublic void run() {synchronized(lock){try {while(number<=100){if(number%3==0){System.out.println("ThreadC:" + number);number++;lock.notifyAll();}else{lock.wait();}}} catch (InterruptedException e) {e.printStackTrace();}}}});ThreadA.start();ThreadB.start();ThreadC.start();}
}
对于可能被阻塞的线程(使用 wait()
, sleep()
, 或 join()
方法等),当另一个线程调用其 interrupt()
方法时,该线程将抛出一个 InterruptedException
。因此需要catch住异常。
使用Semaphore信号量机制
但是注意到number=100的时候,线程B和线程C也会进入循环,当获得信号量的时候就会打印出101和102,因此要在打印之前在加个判断。
public class Test2 {public static final Semaphore A = new Semaphore(1);public static final Semaphore B = new Semaphore(0);public static final Semaphore C = new Semaphore(0);public static int number =1;public static void main(String[] args) {Thread ThreadA=new Thread(new Runnable() {@Overridepublic void run() {try {while(number<=100){A.acquire();if(number<=100)System.out.println("ThreadA:" + number);number++;B.release();}} catch (InterruptedException e) {e.printStackTrace();}}});Thread ThreadB=new Thread(new Runnable() {@Overridepublic void run() {try {while(number<=100){B.acquire();if(number<=100)System.out.println("ThreadB:" + number);number++;C.release();}} catch (InterruptedException e) {e.printStackTrace();}}});Thread ThreadC=new Thread(new Runnable() {@Overridepublic void run() {try {while(number<=100){C.acquire(); if(number<=100)System.out.println("ThreadC:" + number);number++;A.release();}} catch (InterruptedException e) {e.printStackTrace();}}});ThreadA.start();ThreadB.start();ThreadC.start();}
}
2、三个线程轮流打印100次A B C
也就是说,每个线程打印100次
public class printABC {private static int state = 0;// 三个信号量对象,分别表示A、B、C三个线程的初始许可数private static final Semaphore A = new Semaphore(1);private static final Semaphore B = new Semaphore(0);private static final Semaphore C = new Semaphore(0);public static void main(String[] args) {// 创建三个线程Thread threadA = new Thread(new Runnable() {@Overridepublic void run() {try {// 循环100次for (int i = 0; i < 100; i++) {// 获取许可A.acquire();// 打印字母System.out.println("A"+i);// 修改状态state++;// 释放许可B.release();}} catch (InterruptedException e) {e.printStackTrace();}}});Thread threadB = new Thread(new Runnable() {@Overridepublic void run() {try {for (int i = 0; i < 100; i++) {B.acquire();System.out.println("B"+i);state++;C.release();}} catch (InterruptedException e) {e.printStackTrace();}}});Thread threadC = new Thread(new Runnable() {@Overridepublic void run() {try {for (int i = 0; i < 100; i++) {C.acquire();System.out.println("C"+i);state++;A.release();}} catch (InterruptedException e) {e.printStackTrace();}}});// 启动三个线程threadA.start();threadB.start();threadC.start();}}