时间:2021-05-20
使用wait()和notify()实现Java多线程通信:两个线程交替打印A和B,如ABABAB
public class Test { public static void main(String[] args) { final PrintAB print = new PrintAB(); new Thread(new Runnable() { public void run(){ for(int i=0;i<5;i++) { print.printA(); } } }).start(); new Thread(new Runnable() { public void run() { for(int i=0;i<5;i++) { print.printB(); } } }).start(); } } class PrintAB{ private boolean flag = true; public synchronized void printA () { while(!flag) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print("A"); flag = false; this.notify(); } public synchronized void printB () { while(flag) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print("B"); flag = true; this.notify(); } }补充知识:Java多个线程顺序打印数字
要求
启动N个线程, 这N个线程要不间断按顺序打印数字1-N. 将问题简化为3个线程无限循环打印1到3
方法一: 使用synchronized
三个线程无序竞争同步锁, 如果遇上的是自己的数字, 就打印. 这种方式会浪费大量的循环
public class TestSequential1 { private volatile int pos = 1; private volatile int count = 0; public void one(int i) { synchronized (this) { if (pos == i) { System.out.println("T-" + i + " " + count); pos = i % 3 + 1; count = 0; } else { count++; } } } public static void main(String[] args) { TestSequential1 demo = new TestSequential1(); for (int i = 1; i <=3; i++) { int j = i; new Thread(()->{ while(true) { demo.one(j); } }).start(); } }}输出
T-1 0T-2 5793T-3 5285T-1 2616T-2 33T-3 28T-1 22T-2 44T-3 6T-1 881T-2 118358T-3 247380T-1 30803T-2 29627T-3 52044...方法二: 使用synchronized配合wait()和notifyAll()
竞争同步锁时使用wait()和notifyAll(), 可以避免浪费循环
public class TestSequential4 { private volatile int pos = 1; private volatile int count = 0; private final Object obj = new Object(); public void one(int i) { System.out.println(i + " try"); synchronized (obj) { System.out.println(i + " in"); try { while (pos != i) { count++; System.out.println(i + " wait"); obj.wait(); } System.out.println("T-" + i + " " + count); pos = i % 3 + 1; count = 0; obj.notifyAll(); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { TestSequential4 demo = new TestSequential4(); for (int i = 3; i >=1; i--) { int j = i; new Thread(()->{ while(true) { demo.one(j); } }).start(); } }}输出
3 try3 in3 wait2 try2 in2 wait1 try1 inT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 21 try1 in1 waitT-2 12 try2 in2 waitT-3 13 try3 in3 wait2 waitT-1 2...方法三: 使用可重入锁
用Lock做, 非公平锁, 三个线程竞争, 如果遇上的是自己的数字, 就打印. 这种方式也会浪费大量的循环
public class TestSequential2 { private final Lock lock = new ReentrantLock(); private volatile int pos = 1; private volatile int count = 0; public void one(int i) { lock.lock(); if (pos == i) { System.out.println("T-" + i + " " + count); pos = i % 3 + 1; count = 0; } else { count++; } lock.unlock(); } public static void main(String[] args) { TestSequential2 demo = new TestSequential2(); for (int i = 1; i <=3; i++) { int j = i; new Thread(()->{ while(true) { demo.one(j); } }).start(); } }}输出
T-1 0T-2 0T-3 323T-1 54T-2 68964T-3 97642T-1 6504T-2 100603T-3 6989T-1 1313T-2 0T-3 183741T-1 233T-2 5081T-3 164367..方法四: 使用可重入锁, 启用公平锁
和3一样, 但是使用公平锁, 这种情况下基本上可以做到顺序执行, 偶尔会产生多一次循环
private final Lock lock = new ReentrantLock(true);
输出
T-1 0T-2 0T-3 0T-1 0T-2 0T-3 0T-1 0T-2 0T-3 0T-1 0T-2 0T-3 1T-1 1T-2 1T-3 1...方法五: 使用Condition
每个线程如果看到不是自己的计数, 就await(), 如果是自己的计数, 就完成打印动作, 再signalAll()所有其他线程去继续运行, 自己在下一个循环后, 即使又继续执行, 也会因为计数已经变了而await.
如果ReentrantLock构造参数使用true, 可以基本消除 ~await 这一步的输出.
public class ReentrantLockCondition2 { private static Lock lock = new ReentrantLock(); private static Condition condition = lock.newCondition(); private volatile int state = 1; private void handle(int state) { lock.lock(); try { while(true) { while(this.state != state) { System.out.println(state + " ~await"); condition.await(); } System.out.println(state); this.state = state % 3 + 1; condition.signalAll(); System.out.println(state + " await"); condition.await(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public static void main(String[] args) { ReentrantLockCondition2 rlc = new ReentrantLockCondition2(); new Thread(()->rlc.handle(1)).start(); new Thread(()->rlc.handle(2)).start(); new Thread(()->rlc.handle(3)).start(); }}方法六: 使用多个Condition
给每个线程不同的condition. 这个和4的区别是, 可以用condition.signal()精确地通知对应的线程继续执行(在对应的condition上await的线程, 可能是多个). 这种情况下是可以多个线程都不unlock锁的情况下进行协作的. 注意下面的while(true)循环是在lock.lock()方法内部的.
public class ReentrantLockCondition { private static Lock lock = new ReentrantLock(); private static Condition[] conditions = {lock.newCondition(), lock.newCondition(), lock.newCondition()}; private volatile int state = 1; private void handle(int state) { lock.lock(); try { while(true) { while(this.state != state) { conditions[state - 1].await(); } System.out.println(state); this.state = state % 3 + 1; conditions[this.state - 1].signal(); conditions[state - 1].await(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public static void main(String[] args) { ReentrantLockCondition rlc = new ReentrantLockCondition(); new Thread(()->rlc.handle(1)).start(); new Thread(()->rlc.handle(2)).start(); new Thread(()->rlc.handle(3)).start(); }}以上这篇Java多线程通信:交替打印ABAB实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了java实现多线程交替打印的具体代码,供大家参考,具体内容如下notify+wait实现importorg.junit.Test;impor
本文实例为大家分享了java实现多线程交替打印两个数的具体代码,供大家参考,具体内容如下方法1、使用wait和notifypackagecom.thread;p
实现需求:两个线程交替打印1,0,打印10轮java多线程口诀:高内聚,低耦合线程操作资源类判断干活通知防止虚假唤醒方式一:使用synchronized和Obj
在上篇文章给大家介绍了java多线程的实现方式,通过本文给大家介绍java多线程实例,对java多线程感兴趣的朋友一起学习吧首先给大家说下多线程的优缺点多线程的
Java多线程中线程间的通信一、使用while方式来实现线程之间的通信packagecom.ietree.multithread.sync;importjava