第一种,传统方式wait和notify
- public static void main(String[] args) {
- Object o = new Object();
- char[] chars1 = "12345".toCharArray();
- char[] chars2 = "ABCDE".toCharArray();
- new Thread(()->{
- synchronized (o) {
- for (int i = 0; i < chars1.length; i++) {
- System.out.print(chars1[i]);
- o.notify();
- try {
- o.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- o.notify();
- }
-
- },"t1").start();
- new Thread(()->{
- synchronized (o) {
- for (int i = 0; i < chars2.length; i++) {
- System.out.print(chars2[i]);
- o.notify();
- try {
- o.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- o.notify();
- }
-
- },"t2").start();
- }
如上代码:思考两个问题wait和notify的顺序可以颠倒吗,为什么for循环后还要notify?
wait和notify的顺序不能颠倒,因为如果wait在前当前线程就在等待队列中,永远不会被唤醒了
for循环后还要加notify,是因为两个线程无论哪个最后执行,都会其中有一个在wait,所以要结束等待要加notify,要不程序永不停止
注意:这种方式保证不了线程t1先于t2执行,可能t2执行后t1在执行,引入门闩解决
第二种方式:CountDownLatch
- public static void main(String[] args) {
- Object o = new Object();
- CountDownLatch latch = new CountDownLatch(1);
- char[] chars1 = "12345".toCharArray();
- char[] chars2 = "ABCDE".toCharArray();
- new Thread(()->{
- latch.countDown();
- synchronized (o) {
- for (int i = 0; i < chars1.length; i++) {
- System.out.print(chars1[i]);
- o.notify();
- try {
- o.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- o.notify();
- }
- },"t1").start();
- new Thread(()->{
- try {
- latch.await();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- synchronized (o) {
- for (int i = 0; i < chars2.length; i++) {
- System.out.print(chars2[i]);
- o.notify();
- try {
- o.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- o.notify();
- }
-
- },"t2").start();
- }
第三种方式:LockSupport
- static Thread t1 = null,t2 = null;
- public static void main(String[] args) {
- char[] chars1 = "123456".toCharArray();
- char[] chars2 = "ABCDEF".toCharArray();
-
- t1 = new Thread(()->{
- for (char a:chars1) {
- System.out.print(a);
- LockSupport.unpark(t2);//唤醒t2
- LockSupport.park();//阻塞本线程
- }
- });
- t2 = new Thread(()->{
- for(char b:chars2){
- LockSupport.park();
- System.out.print(b);
- LockSupport.unpark(t1);
- }
- });
- t1.start();
- t2.start();
- }
第四种方式:ReentrantLock
- public static void main(String[] args) {
- ReentrantLock lock = new ReentrantLock();
- Condition condition1 = lock.newCondition();
- Condition condition2 = lock.newCondition();
- char[] chars1 = "12345".toCharArray();
- char[] chars2 = "ABCDE".toCharArray();
- new Thread(()->{
- try {
- lock.lock();
- for (int i = 0; i < chars1.length; i++) {
- System.out.print(chars1[i]);
- condition2.signal();
- condition1.await();
- }
- condition2.signal();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }finally {
- lock.unlock();
- }
- },"t1").start();
- new Thread(()->{
- try {
- lock.lock();
- for (int i = 0; i < chars2.length; i++) {
- System.out.print(chars2[i]);
- condition1.signal();
- condition2.await();
- }
- condition1.signal();
- }catch (InterruptedException e) {
- e.printStackTrace();
- }finally {
- lock.unlock();
- }
- },"t2").start();
- }
第五种方式:TransferQueue
- public static void main(String[] args) {
- char[] chars1 = "12345".toCharArray();
- char[] chars2 = "ABCDE".toCharArray();
- TransferQueue<Character> queue = new LinkedTransferQueue<>();
- new Thread(()->{
- for (int i = 0; i < chars1.length; i++) {
- try {
- System.out.print(queue.take());
- queue.transfer(chars1[i]);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- },"t1").start();
- new Thread(()->{
- for (int i = 0; i < chars2.length; i++) {
- try {
- queue.transfer(chars2[i]);
- System.out.print(queue.take());
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- },"t2").start();
- }
第六种方式:枚举类
- enum ReadToRun{T1,T2};
- static volatile ReadToRun r = ReadToRun.T1;
-
- public static void main(String[] args) {
- char[] chars1 = "123456".toCharArray();
- char[] chars2 = "ABCDEF".toCharArray();
- new Thread(()->{
- for(char a:chars1){
- while(r!=ReadToRun.T1){}//若r不等于T1,自旋空转
- System.out.print(a);
- r = ReadToRun.T2;
- }
- },"t1").start();
- new Thread(()->{
- for(char b:chars2){
- while(r!=ReadToRun.T2){}//若r不等于T2,自旋空转
- System.out.println(b);
- r = ReadToRun.T1;
- }
- },"t2").start();
- }