标签:获取 一个 exchange await code ace tac div 通信
1.常用的方法
sleep()该线程进入等待状态,不释放锁
wait() 该线程进入等待状态,释放锁
notify() 随机唤醒一个线程
notifyAll() 唤醒全部线程
2.线程之间的通信
a.两个线程之间的通信
public class ThreadExchange { @Test public void test2Thread() { MyPrint myPrint = new MyPrint(); new Thread(new Runnable() { @Override public void run() { while (true) { try { myPrint.printA(); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); new Thread(new Runnable() { @Override public void run() { while (true) { try { myPrint.printB(); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } } class MyPrint { private Integer flag = 0; public synchronized void printA() throws InterruptedException { if (flag != 0) { this.wait(); } System.out.print("a"); System.out.print("a"); System.out.print("a"); System.out.print("a"); System.out.println("a"); flag = 1; this.notify(); } public synchronized void printB() throws InterruptedException { if (flag != 1) { this.wait(); } System.out.print("b"); System.out.print("b"); System.out.print("b"); System.out.print("b"); System.out.println("b"); flag = 0; this.notify(); } }
b.三个以上的线程之间的通信
方式一
public class ThreadExchange { @Test public void test2Thread() { MyPrint myPrint = new MyPrint(); new Thread(new Runnable() { @Override public void run() { while (true) { try { myPrint.printA(); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); new Thread(new Runnable() { @Override public void run() { while (true) { try { myPrint.printB(); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } } class MyPrint { private Integer flag = 0; public synchronized void printA() throws InterruptedException { if (flag != 0) { this.wait(); } System.out.print("a"); System.out.print("a"); System.out.print("a"); System.out.print("a"); System.out.println("a"); flag = 1; this.notify(); } public synchronized void printB() throws InterruptedException { if (flag != 1) { this.wait(); } System.out.print("b"); System.out.print("b"); System.out.print("b"); System.out.print("b"); System.out.println("b"); flag = 0; this.notify(); } } 2.三个以上的线程之间的通信 public class ThreadExchange { @Test public void test2Thread() { final MyPrint p = new MyPrint(); new Thread() { public void run() { while (true) { try { p.print1(); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); new Thread() { public void run() { while (true) { try { p.print2(); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); new Thread() { public void run() { while (true) { try { p.print3(); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } } class MyPrint { private int flag = 1; public void print1() throws InterruptedException { synchronized(this) { while(flag != 1) { this.wait(); } System.out.print("a"); System.out.print("a"); System.out.print("a"); System.out.print("a"); System.out.println("a"); flag = 2; this.notifyAll(); } } public void print2() throws InterruptedException { synchronized (this) { while (flag != 2) { this.wait(); } System.out.print("b"); System.out.print("b"); System.out.print("b"); System.out.print("b"); System.out.println("b"); flag = 3; this.notifyAll(); } } public void print3() throws InterruptedException { synchronized (this) { while (flag != 3) { this.wait(); } System.out.print("c"); System.out.print("c"); System.out.print("c"); System.out.print("c"); System.out.println("c"); flag = 1; this.notifyAll(); } } }
方式二
public class ThreadExchange { @Test public void test3Thread() { final MyPrint p = new MyPrint(); new Thread() { public void run() { while (true) { try { p.print1(); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); new Thread() { public void run() { while (true) { try { p.print2(); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); new Thread() { public void run() { while (true) { try { p.print3(); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } } class MyPrint { private ReentrantLock r = new ReentrantLock(); private Condition c1 = r.newCondition(); private Condition c2 = r.newCondition(); private Condition c3 = r.newCondition(); private int flag = 1; public void print1() throws InterruptedException { r.lock(); // 获取锁 if (flag != 1) { c1.await(); } System.out.print("a"); System.out.print("a"); System.out.print("a"); System.out.print("a"); System.out.println("a"); flag = 2; c2.signal(); r.unlock(); // 释放锁 } public void print2() throws InterruptedException { r.lock(); if (flag != 2) { c2.await(); } System.out.print("b"); System.out.print("b"); System.out.print("b"); System.out.print("b"); System.out.println("b"); flag = 3; c3.signal(); r.unlock(); } public void print3() throws InterruptedException { r.lock(); if (flag != 3) { c3.await(); } System.out.print("c"); System.out.print("c"); System.out.print("c"); System.out.print("c"); System.out.println("c"); flag = 1; c1.signal(); r.unlock(); } }
标签:获取 一个 exchange await code ace tac div 通信
原文地址:https://www.cnblogs.com/codeLei/p/9070747.html