在多线程的开发过程之中最为著名的案例就是生产者与消费者操作,该操作的主要流程如下
- 生产者负责信息内容的生产:
- 每当生产者生产完成一项完整的信息之后,消费者要从这里面取走信息:
- 如果生产者没有生产完,则消费者要等待他生产完成,如果消费者还没有对信息进行消费,则生产者要等待消费者对信息消费完成之后再生产
程序的基本实现
可以将生产者与消费者定义为两个独立的线程类对象,但是对于现在生产的数据 可以使用如下的组成
- 数据一:title=cirry,content=帅哥
- 数据二:title=winnie,content=美女 既然生产者与消费者是两个独立的线程,那么这两个独立的线程之间就需要一个数据的保存集中点,那么可以单独的定义一个Message类
范例:
1package cn.cccc.demo;2class Producer implements Runnable{3 private Message msg;4 public Producer(Message msg){5 this.msg = msg;6 }7 public void run() {8 for(int x = 0; x < 100; x++){9 if(x%2==0){10 this.msg.setTitle("cirry");11 try {12 Thread.sleep(100);13 } catch (InterruptedException e) {14 // TODO Auto-generated catch block15 e.printStackTrace();63 collapsed lines
16 }17 this.msg.setContent("帅哥");18 }else {19 this.msg.setTitle("winnie");20 try {21 Thread.sleep(100);22 } catch (InterruptedException e) {23 // TODO Auto-generated catch block24 e.printStackTrace();25 }26 this.msg.setContent("美女");27 }28 }29 }30}31class Consumer implements Runnable{32 private Message msg;33 public Consumer(Message msg){34 this.msg = msg;35 }36 public void run(){37 for(int x = 0; x < 100 ; x ++){38
39 try {40 Thread.sleep(10);41 } catch (InterruptedException e) {42 // TODO Auto-generated catch block43 e.printStackTrace();44 }45
46 System.out.println(this.msg.getContent()+ " - " + this.msg.getTitle());47 }48 }49}50class Message {51 private String title;52 private String content;53 public String getTitle() {54 return title;55 }56 public void setTitle(String title) {57 this.title = title;58 }59 public String getContent() {60 return content;61 }62 public void setContent(String content) {63 this.content = content;64 }65
66}67public class ThreadDemo {68 public static void main(String[] args) {69 Message msg = new Message();70 new Thread(new Producer(msg)).start(); //启动生产者线程71 new Thread(new Consumer(msg)).start(); //启动消费者线程72 }73}74
75//执行结果:76//null - cirry77//帅哥 - winnie78//美女 - cirry
通过整个代码的执行你会发现此时有两个问题:
- 问题一:数据不同步了
- 问题二:生产一个取走一个,但是发现有了重复生产和重复取出问题。
解决数据同步问题
如果想要解决问题, 首先要解决的就是数据同步的处理问题,如果要想解决数据同步最简的就是用synchronize同步代码块或同步方法,于是这个时候对于同步的处理就可以直接在Message类中完成。 范例:解决同步操作问题
1package cn.cccc.demo;2class Producer implements Runnable{3 private Message msg;4 public Producer(Message msg){5 this.msg = msg;6 }7 public void run() {8 for(int x = 0; x < 100; x++){9 if(x%2==0){10 this.msg.set("cirry","帅哥");11 }else {12 this.msg.set("winnie","美女");13 }14 }15 }37 collapsed lines
16}17class Consumer implements Runnable{18 private Message msg;19 public Consumer(Message msg){20 this.msg = msg;21 }22 public void run(){23 for(int x = 0; x < 100 ; x ++){24 System.out.println(this.msg.get());25 }26 }27}28class Message {29 private String title;30 private String content;31 public synchronized void set(String title , String content){32 this.title = title;33 this.content = content;34 }35 public synchronized String get(){36 return this.title + " - " + this.content;37 }38}39public class ThreadDemo {40 public static void main(String[] args) {41 Message msg = new Message();42 new Thread(new Producer(msg)).start(); //启动生产者线程43 new Thread(new Consumer(msg)).start(); //启动消费者线程44 }45}46
47//运行结果:48//winnie - 美女49//winnie - 美女50//winnie - 美女51//...52//cirry - 帅哥是彻底消失不见了
在进行同步处理的时候肯定需要有一个同步的处理对象,那么此时肯定要将同步操作交由Message类处理最合适。这个时候发现数据已经可以正常的保持一致了,但是对于重复操作的问题依然存在。
线程等待与唤醒
如果仙子要想解决生产者与箱费这的问题,那么最好的解决方案就是使用等待与唤醒机制。而对于等待与唤醒的机制主要依靠的是Object类中提供的方法处理:
- 等待机制:
- 死等:public final void wait() throws InterruptedException ;
- 设置等待时间:public final void wait(long timeout) throws InterruptedException ;
- 设置等待时间 :public final void wait(long timeout, int nanos) throws InterruptedException;
- 唤醒第一个等待线程:public final void notify();
- 唤醒全部等待线程:public final void notifyAll(); 如果此时有若干个等待线程的话,那么notify()表示的是唤醒第一个等待的,而其他的线程继续等待。而notifyAll()表示会唤醒所有等待的线程,哪个线程的优先级高就有可能先执行。 对于当前的问题主要的解决应该通过Message类来解决。 范例:
1package cn.cccc.demo;2class Producer implements Runnable{3 private Message msg;4 public Producer(Message msg){5 this.msg = msg;6 }7 public void run() {8 for(int x = 0; x < 100; x++){9 if(x%2 == 0){10 this.msg.set("cirry","帅哥");11 }else {12 this.msg.set("winnie","美女");13 }14 }15 }69 collapsed lines
16}17class Consumer implements Runnable{18 private Message msg;19 public Consumer(Message msg){20 this.msg = msg;21 }22 public void run(){23 for(int x = 0; x < 100 ; x ++){24 System.out.println(this.msg.get());25 }26 }27}28class Message {29 private String title;30 private String content;31 private boolean flag = true; //表示生产或消费的形式。32 // flag = true; 允许生产,但是不允许消费33 // flag = false; 允许消费,但是不允许生产34 public synchronized void set(String title , String content){35 try {36 Thread.sleep(100);37 } catch (InterruptedException e1) {38 // TODO Auto-generated catch block39 e1.printStackTrace();40 }41 if(this.flag == false){42 try {43 super.wait();44 } catch (InterruptedException e) {45 // TODO Auto-generated catch block46 e.printStackTrace();47 }48 this.title = title;49 this.content = content;50 }51
52 this.flag = false;//已经生产过了53 super.notify(); //唤醒等待的线程54 }55 public synchronized String get(){56 try {57 Thread.sleep(100);58 } catch (InterruptedException e1) {59 // TODO Auto-generated catch block60 e1.printStackTrace();61 }62 if(this.flag == true){// 还未生产需要等待63 try{64 super.wait();65 }catch(InterruptedException e){66 e.printStackTrace();67 }68 }69 try{70 return this.title + " - " + this.content;71 }finally{ //不管如何,都要执行72 this.flag = true; //表示继续生产73 super.notify();74 }75
76 }77}78public class ThreadDemo {79 public static void main(String[] args) {80 Message msg = new Message();81 new Thread(new Producer(msg)).start(); //启动生产者线程82 new Thread(new Consumer(msg)).start(); //启动消费者线程83 }84}
执行结果: cirry - 帅哥 winnie - 美女 … 这个方案就是多线程开发过程之中最原始的处理方案,整个的等待,同步,唤醒机智都是由开发者自行通过原生代码实现控制。