用好多线程重要的是协调线程调用资源,下面看一个实例:多线程
三个线程,第一个线程循环输出三条信息,第二个线程循环输出两条信息,第三个线程循环输出一条信息,按照一二三线程顺序依次输出,而后按照这个顺序循环五回。ide
多线程依次输出,依次控制就是多线程须要访问的共享资源。this
public class ThreadTest { public static class ChildRun implements Runnable { private ThreadTest thread; public ChildRun(ThreadTest thread) { this.thread = thread; } @Override public void run() { for (int i = 0; i < 5; i ++) { thread.showChild(); } } } public static class GrandsonRun implements Runnable { private ThreadTest thread; public GrandsonRun(ThreadTest thread) { this.thread = thread; } @Override public void run() { for (int i = 0; i < 5; i ++) { thread.showGrandson(); } } } private int status = 0; public synchronized void showParent() { if (0 != this.status) { try { this.wait(); if (0 != this.status) { this.showParent(); return ; } } catch (InterruptedException e) { e.printStackTrace(); } } for (int i = 0; i < 3; i ++) { System.out.println("parent"); } this.notifyAll(); this.status = 1; } public synchronized void showChild() { if (1 != this.status) { try { this.wait(); if (1 != this.status) { this.showChild(); return ; } } catch (InterruptedException e) { e.printStackTrace(); } } for (int i = 0; i < 2; i ++) { System.out.println("child"); } this.notifyAll(); this.status = 2; } public synchronized void showGrandson() { if (2 != this.status) { try { this.wait(); if (2 != this.status) { this.showGrandson(); return ; } } catch (InterruptedException e) { e.printStackTrace(); } } for (int i = 0; i < 1; i ++) { System.out.println("grandson"); } this.notifyAll(); this.status = 0; } public static void main(String[] args) { ThreadTest thread = new ThreadTest(); ExecutorService service = Executors.newCachedThreadPool(); service.execute(new ChildRun(thread)); service.execute(new GrandsonRun(thread)); service.shutdown(); for (int i = 0; i < 5; i ++) { thread.showParent(); } } }
输出:spa
parent
parent
parent
child
child
grandson
parent
parent
parent
child
child
grandson
parent
parent
parent
child
child
grandson
parent
parent
parent
child
child
grandson
parent
parent
parent
child
child
grandson
这里的ThreadTest类包含两个Runnbale,就是其中两个线程,而这个类自己就是一个资源共享类,里面的status就是控制顺序的状态。主线程内首先建立执行了第二三个线程,使这两个线程先进入等待状态,而后再执行主线程的,待主线程完毕以后唤醒其余两条线程,这时候可能会进入不应进入的线程三,因此在wait后面又进行了一次判断,使它能继续保持等待状态让出执行权,直到状态符合的状况下进行执行。线程
注意几个问题:code
一、必定要让等待的线程先执行,否则会出现主线程通知时,没有找到等待的线程而产生的阻塞。blog
二、wait以后的再次判断很重要,由于在超过两个线程的状况下,不肯定是否是通知的该线程。资源