static void stopByFlag() { ARunnable ar = new ARunnable(); new Thread(ar).start(); ar.tellToStop(); } static class ARunnable implements Runnable { volatile boolean stop; void tellToStop() { stop = true; } @Override public void run() { println("进入不可中止区域 1。。。"); doingLongTime(5); println("退出不可中止区域 1。。。"); println("检测标志stop = %s", String.valueOf(stop)); if (stop) { println("中止执行"); return; } println("进入不可中止区域 2。。。"); doingLongTime(5); println("退出不可中止区域 2。。。"); } }
static void pauseByFlag() { BRunnable br = new BRunnable(); new Thread(br).start(); br.tellToPause(); sleep(8); br.tellToResume(); } static class BRunnable implements Runnable { volatile boolean pause; void tellToPause() { pause = true; } void tellToResume() { synchronized (this) { this.notify(); } } @Override public void run() { println("进入不可暂停区域 1。。。"); doingLongTime(5); println("退出不可暂停区域 1。。。"); println("检测标志pause = %s", String.valueOf(pause)); if (pause) { println("暂停执行"); try { synchronized (this) { this.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } println("恢复执行"); } println("进入不可暂停区域 2。。。"); doingLongTime(5); println("退出不可暂停区域 2。。。"); } }
static void jqByJoin() { CRunnable cr = new CRunnable(); Thread t = new Thread(cr); t.start(); sleep(1); try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } println("终于轮到我了"); } static class CRunnable implements Runnable { @Override public void run() { println("进入不可暂停区域 1。。。"); doingLongTime(5); println("退出不可暂停区域 1。。。"); } }
static void stopByInterrupt() { DRunnable dr = new DRunnable(); Thread t = new Thread(dr); t.start(); sleep(2); t.interrupt(); } static class DRunnable implements Runnable { @Override public void run() { println("进入暂停。。。"); try { sleep2(5); } catch (InterruptedException e) { println("收到中断异常。。。"); println("作一些相关处理。。。"); } println("继续执行或选择退出。。。"); } }
static final int COUNT = 20; public static void main(String[] args) throws Exception { new Thread(new Teacher(cdl)).start(); sleep(1); for (int i = 0; i < COUNT; i++) { new Thread(new Student(i, cdl)).start(); } synchronized (ThreadCo1.class) { ThreadCo1.class.wait(); } } static CountDownLatch cdl = new CountDownLatch(COUNT); static class Teacher implements Runnable { CountDownLatch cdl; Teacher(CountDownLatch cdl) { this.cdl = cdl; } @Override public void run() { println("老师发卷子。。。"); try { cdl.await(); } catch (InterruptedException e) { e.printStackTrace(); } println("老师收卷子。。。"); } } static class Student implements Runnable { CountDownLatch cdl; int num; Student(int num, CountDownLatch cdl) { this.num = num; this.cdl = cdl; } @Override public void run() { println("学生(%d)写卷子。。。", num); doingLongTime(); println("学生(%d)交卷子。。。", num); cdl.countDown(); } }
static final int COUNT = 5; public static void main(String[] args) throws Exception { for (int i = 0; i < COUNT; i++) { new Thread(new Staff(i, cb)).start(); } synchronized (ThreadCo2.class) { ThreadCo2.class.wait(); } } static CyclicBarrier cb = new CyclicBarrier(COUNT, new Singer()); static class Singer implements Runnable { @Override public void run() { println("为你们唱歌。。。"); } } static class Staff implements Runnable { CyclicBarrier cb; int num; Staff(int num, CyclicBarrier cb) { this.num = num; this.cb = cb; } @Override public void run() { println("员工(%d)出发。。。", num); doingLongTime(); println("员工(%d)到达地点一。。。", num); try { cb.await(); } catch (Exception e) { e.printStackTrace(); } println("员工(%d)再出发。。。", num); doingLongTime(); println("员工(%d)到达地点二。。。", num); try { cb.await(); } catch (Exception e) { e.printStackTrace(); } println("员工(%d)再出发。。。", num); doingLongTime(); println("员工(%d)到达地点三。。。", num); try { cb.await(); } catch (Exception e) { e.printStackTrace(); } println("员工(%d)结束。。。", num); } }
public static void main(String[] args) throws Exception { new Thread(new Staff("大胖", new Tool("笤帚", "扫地"), ex)).start(); new Thread(new Staff("小白", new Tool("抹布", "擦桌"), ex)).start(); synchronized (ThreadCo3.class) { ThreadCo3.class.wait(); } } static Exchanger<Tool> ex = new Exchanger<>(); static class Staff implements Runnable { String name; Tool tool; Exchanger<Tool> ex; Staff(String name, Tool tool, Exchanger<Tool> ex) { this.name = name; this.tool = tool; this.ex = ex; } @Override public void run() { println("%s拿的工具是[%s],他开始[%s]。。。", name, tool.name, tool.work); doingLongTime(); println("%s开始交换工具。。。", name); try { tool = ex.exchange(tool); } catch (Exception e) { e.printStackTrace(); } println("%s的工具变为[%s],他开始[%s]。。。", name, tool.name, tool.work); } } static class Tool { String name; String work; Tool(String name, String work) { this.name = name; this.work = work; } }
static final int COUNT = 6; public static void main(String[] args) throws Exception { new Thread(new Challenger("张三")).start(); new Thread(new Challenger("李四")).start(); new Thread(new Challenger("王五")).start(); new Thread(new Challenger("赵六")).start(); new Thread(new Challenger("大胖")).start(); new Thread(new Challenger("小白")).start(); synchronized (ThreadCo4.class) { ThreadCo4.class.wait(); } } static Phaser ph = new Phaser() { protected boolean onAdvance(int phase, int registeredParties) { println2("第(%d)局,剩余[%d]人", phase, registeredParties); return registeredParties == 0 || (phase != 0 && registeredParties == COUNT); }; }; static class Challenger implements Runnable { String name; int state; Challenger(String name) { this.name = name; this.state = 0; } @Override public void run() { println("[%s]开始挑战。。。", name); ph.register(); int phase = 0; int h; while (!ph.isTerminated() && phase < 100) { doingLongTime(5); if (state == 0) { if (Decide.goon()) { h = ph.arriveAndAwaitAdvance(); if (h < 0) println("No%d.[%s]继续,但已胜利。。。", phase, name); else println("No%d.[%s]继续at(%d)。。。", phase, name, h); } else { state = -1; h = ph.arriveAndDeregister(); println("No%d.[%s]退出at(%d)。。。", phase, name, h); } } else { if (Decide.revive()) { state = 0; h = ph.register(); if (h < 0) println("No%d.[%s]复活,但已失败。。。", phase, name); else println("No%d.[%s]复活at(%d)。。。", phase, name, h); } else { println("No%d.[%s]没有复活。。。", phase, name); } } phase++; } if (state == 0) { ph.arriveAndDeregister(); } println("[%s]结束。。。", name); } } static class Decide { static boolean goon() { return random(9) > 4; } static boolean revive() { return random(9) < 5; } }
public static void main(String[] args) { Queue queue = new Queue(); new Thread(new Producer(queue)).start(); new Thread(new Producer(queue)).start(); new Thread(new Consumer(queue)).start(); } static class Producer implements Runnable { Queue queue; Producer(Queue queue) { this.queue = queue; } @Override public void run() { try { for (int i = 0; i < 10000; i++) { doingLongTime(); queue.putEle(random(10000)); } } catch (Exception e) { e.printStackTrace(); } } } static class Consumer implements Runnable { Queue queue; Consumer(Queue queue) { this.queue = queue; } @Override public void run() { try { for (int i = 0; i < 10000; i++) { doingLongTime(); queue.takeEle(); } } catch (Exception e) { e.printStackTrace(); } } } static class Queue { Lock lock = new ReentrantLock(); Condition prodCond = lock.newCondition(); Condition consCond = lock.newCondition(); final int CAPACITY = 10; Object[] container = new Object[CAPACITY]; int count = 0; int putIndex = 0; int takeIndex = 0; public void putEle(Object ele) throws InterruptedException { try { lock.lock(); while (count == CAPACITY) { println("队列已满:%d,生产者开始睡大觉。。。", count); prodCond.await(); } container[putIndex] = ele; println("生产元素:%d", ele); putIndex++; if (putIndex >= CAPACITY) { putIndex = 0; } count++; println("通知消费者去消费。。。"); consCond.signalAll(); } finally { lock.unlock(); } } public Object takeEle() throws InterruptedException { try { lock.lock(); while (count == 0) { println("队列已空:%d,消费者开始睡大觉。。。", count); consCond.await(); } Object ele = container[takeIndex]; println("消费元素:%d", ele); takeIndex++; if (takeIndex >= CAPACITY) { takeIndex = 0; } count--; println("通知生产者去生产。。。"); prodCond.signalAll(); return ele; } finally { lock.unlock(); } } }
推荐阅读:javascript