码迷,mamicode.com
首页 > 编程语言 > 详细

黑马程序员——餐桌上的多线程问题

时间:2014-05-15 11:09:46      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:style   class   java   c   tar   color   

餐桌上的多线程问题

生产者和消费者模型

         设想有一天你和一群朋友去餐馆吃饭,这是个典型的同步例子,你和你的朋友们共享着餐桌这个资源。想象一下,当你举起筷子准备夹菜的时候,却发现菜碟子被人端走了~这正是同步要解决的问题,必须保证在某人夹菜的时候,其他人不能把菜盘子端走。Java中使用加锁的机制,使用Synchronized来保证代码执行的时候不受共享资源的其他人的干扰。
     而餐厅的服务员根据你的订单给你上菜,当菜吃完了通知服务员给你们上菜,这又是个典型的生产者消费者模型。下面用内部类简单实现下这个问题。
 
package moreCoding1;
class Person {
          String name;
          Person ( String name){
                    thisnamename;
           }
}
class Shop{
           private Person
           p1new Person( "服务员" ),
           p2new Person( "张三" ),     
           p3new Person( "李四" );     
           private boolean flag= false;
          Thread thread1new Thread( new Runnable(){
                    public void run () {
                              while (true ){
                                       while (flag == true){
                                                 try {
                                                          wait ();
                                                 } catch (Exception e ) {
                                                 }
                                       }
                                       synchronized ( this ) {
                                                System . out. println( p1name"上了" +">>>" +1 +"个菜" );
                                                 flagtrue;
                                                notifyAll ();
                                       } }}}) ;
          Thread thread2new Thread( new Runnable(){
                    public void run () {
                              while (true ){
                                       while (flag == false){
                                                 try {
                                                          wait ();
                                                 } catch (Exception e ) {
                                                 }
                                       }
                                       synchronized ( this ) {
                                                System . out. println( p2name"吃了" +"<<<<" +1 +"个菜" );
                                                 flagfalse;
                                                notifyAll ();
                                       } }}}) ;
          Thread thread3new Thread( new Runnable(){
                    public void run () {
                              // TODO Auto-generated method stub
                              while (true ){
                                       while (flag == false){
                                                 try {
                                                          wait ();
                                                 } catch (Exception e ) {
                                                 }}
                                       synchronized ( this ) {
                                                System . out. println( p3name"吃了" +"~~~~" +1 +"个菜" );
                                                 flagfalse;
                                                notifyAll ();
                                       } }}}) ;
          Shop (){
                    thread1start ();
                    thread2start ();
                    thread3start ();
           }
}
public class MyThread {
public static void main ( String[] args ) {
           new Shop() ;
}
}
//
/*张三吃了<<<<1个菜
服务员上了>>>1个菜
张三吃了<<<<1个菜
服务员上了>>>1个菜
李四吃了~~~~1个菜
服务员上了>>>1个菜
李四吃了~~~~1个菜
服务员上了>>>1个菜
李四吃了~~~~1个菜*/
死锁

     假如发生了这样的问题,筷子不够,筷子的跟数只有你们人数那么多,那么每个人吃饭都要先拿左手的筷子,然后再设法拿到右边的筷子,当你发现每个人左手都拿着筷子等右边的筷子的时候,死锁就产生了。

黑马程序员——餐桌上的多线程问题,布布扣,bubuko.com

黑马程序员——餐桌上的多线程问题

标签:style   class   java   c   tar   color   

原文地址:http://www.cnblogs.com/shuawang/p/3728894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!