多线程--线程间的通信

 

 

 

 

线程间的通讯:
其实就是多个线程在操作同一个资源。
但是操作动作不同

 

例子:

需求:模拟简单卖票系统(输入一个人,紧接着输出一个人)

 

[java] view plaincopy

  1. class Res  
  2. {  
  3.     String name;  
  4.     String sex;  
  5. }  
  6. class Input  implements Runnable  
  7. {  
  8.     private Res r;  
  9.     private int t=0;  
  10.     Input(Res r)  
  11.     {  
  12.         this.r=r;  
  13.     }  
  14.     public void run()  
  15.     {  
  16.         while(true)  
  17.         {  
  18.             if(t==1)  
  19.             {  
  20.                 r.name="nike";  
  21.                 r.sex="man";  
  22.             }  
  23.             else   
  24.             {  
  25.                 r.name="丽丽";  
  26.                 r.sex="女女";  
  27.             }  
  28.             t=(t+1)%2;  
  29.         }  
  30.     }  
  31. }  
  32. class Output  implements Runnable  
  33. {  
  34.     private Res r;  
  35.     Output(Res r)  
  36.     {  
  37.         this.r=r;  
  38.     }  
  39.     public void run()  
  40.     {  
  41.         while(true)  
  42.         {  
  43.             System.out.println("output....."+r.name+"+++"+r.sex);  
  44.         }  
  45.     }  
  46. }  
  47. class InputOutputDemo  
  48. {  
  49.     public static void main(String[] args)  
  50.     {  
  51.         Res r=new Res();  
  52.         Input in=new Input(r);  
  53.         Output out=new Output(r);  
  54.         Thread t1=new Thread(in);  
  55.         Thread t2=new Thread(out);  
  56.         t1.start();  
  57.         t2.start();  
  58.     }  
  59. }  

出现了安全问题(输出了丽丽  MAN)

 

同步后

[java] view plaincopy

  1. class Res  
  2. {  
  3.     String name;  
  4.     String sex;  
  5. }  
  6. class Input  implements Runnable  
  7. {  
  8.     private Res r;  
  9.     private int t=0;  
  10.     Input(Res r)  
  11.     {  
  12.         this.r=r;  
  13.     }  
  14.     public void run()  
  15.     {  
  16.         while(true)  
  17.         {  
  18.             synchronized(Res.class)  
  19.             {  
  20.                 if(t==1)  
  21.                 {  
  22.                     r.name="nike";  
  23.                     r.sex="man";  
  24.                 }  
  25.                 else   
  26.                 {  
  27.                     r.name="丽丽";  
  28.                     r.sex="女女";  
  29.                 }  
  30.                 t=(t+1)%2;  
  31.             }  
  32.         }  
  33.     }  
  34. }  
  35. class Output  implements Runnable  
  36. {  
  37.     private Res r;  
  38.     Output(Res r)  
  39.     {  
  40.         this.r=r;  
  41.     }  
  42.     public void run()  
  43.     {  
  44.         while(true)  
  45.         {  
  46.             synchronized(Res.class)  
  47.             {  
  48.                 System.out.println("output....."+r.name+"+++"+r.sex);  
  49.             }  
  50.         }  
  51.     }  
  52. }  
  53. class InputOutputDemo2  
  54. {  
  55.     public static void main(String[] args)  
  56.     {  
  57.         Res r=new Res();  
  58.         Input in=new Input(r);  
  59.         Output out=new Output(r);  
  60.         Thread t1=new Thread(in);  
  61.         Thread t2=new Thread(out);  
  62.         t1.start();  
  63.         t2.start();  
  64.     }  
  65. }  


虽然安全 问题解决了,但并没出现我们想要的一男一女交替的情景

 

 

这是就引进一种方法:等待唤醒机制

 

[java] view plaincopy

  1. class Res  
  2. {  
  3.     String name;  
  4.     String sex;  
  5.     boolean flag;  
  6. }  
  7. class Input  implements Runnable  
  8. {  
  9.     private Res r;  
  10.     private int t=0;  
  11.     Input(Res r)  
  12.     {  
  13.         this.r=r;  
  14.     }  
  15.     public void run()  
  16.     {  
  17.         while(true)  
  18.         {  
  19.             synchronized(r)  
  20.             {  
  21.                 if(r.flag)  
  22.                     try{r.wait();}catch(Exception e){}  
  23.                 if(t==1)  
  24.                 {  
  25.                     r.name="nike";  
  26.                     r.sex="man";  
  27.                 }  
  28.                 else   
  29.                 {  
  30.                     r.name="丽丽";  
  31.                     r.sex="女女";  
  32.                 }  
  33.                 t=(t+1)%2;  
  34.                 r.flag=true;  
  35.                 r.notify();  
  36.             }  
  37.         }  
  38.     }  
  39. }  
  40. class Output  implements Runnable  
  41. {  
  42.     private Res r;  
  43.     Output(Res r)  
  44.     {  
  45.         this.r=r;  
  46.     }  
  47.     public void run()  
  48.     {  
  49.         while(true)  
  50.         {  
  51.             synchronized(r)  
  52.             {  
  53.                 if(!r.flag)  
  54.                     try{r.wait();}catch(Exception e){}  
  55.                 System.out.println("output....."+r.name+"+++"+r.sex);  
  56.                 r.flag=false;  
  57.                 r.notify();  
  58.             }  
  59.         }  
  60.     }  
  61. }  
  62. class InputOutputDemo3  
  63. {  
  64.     public static void main(String[] args)  
  65.     {  
  66.         Res r=new Res();  
  67.         Input in=new Input(r);  
  68.         Output out=new Output(r);  
  69.         Thread t1=new Thread(in);  
  70.         Thread t2=new Thread(out);  
  71.         t1.start();  
  72.         t2.start();  
  73.     }  
  74. }  

 


 

wait:
notify();
notifyAll();

都使用在同步中,因为要对持有监视器(锁)的操作。
所以要使用在同步中,因为只有同步才具有锁。

为什么这些操作线程的凤飞飞要定义Object类中呢?
因为这些方法在操作同步中线程时,都必须要标识它们所操作线程中的锁,
只有同一个锁上的被等待线程,可以被同一个锁上notify唤醒。
不可以被不同锁中的线程进行唤醒。

也就是说,等待和唤醒必须是同一个锁。
而锁可以是任意对象,所以可以被任意对象调用的方法定义Object类中。

 

 

下面进行代码改良:

[java] view plaincopy

  1. class Res  
  2. {  
  3.     private String name;  
  4.     private String sex;  
  5.     private boolean flag;  
  6.     public synchronized void  set(String name,String sex)  
  7.     {  
  8.         if(this.flag)  
  9.           try{this.wait();}catch(Exception e){}  
  10.   
  11.         this.name=name;  
  12.         this.sex=sex;  
  13.   
  14.         this.flag=true;  
  15.           this.notify();  
  16.   
  17.     }  
  18.     public synchronized void out()  
  19.     {  
  20.         if(!this.flag)  
  21.          try{this.wait();}catch(Exception e){}  
  22.         System.out.println("output....."+this.name+"+++"+this.sex);  
  23.         this.flag=false;  
  24.                 this.notify();  
  25.     }  
  26. }  
  27. class Input  implements Runnable  
  28. {  
  29.     private Res r;  
  30.     private int t=0;  
  31.     Input(Res r)  
  32.     {  
  33.         this.r=r;  
  34.     }  
  35.     public void run()  
  36.     {  
  37.         while(true)  
  38.         {  
  39.             synchronized(r)  
  40.             {  
  41.                 if(t==1)  
  42.                     r.set("nike","man");  
  43.                 else   
  44.                     r.set("丽丽","女女女");  
  45.                 t=(t+1)%2;  
  46.             }  
  47.         }  
  48.     }  
  49. }  
  50. class Output  implements Runnable  
  51. {  
  52.     private Res r;  
  53.     Output(Res r)  
  54.     {  
  55.         this.r=r;  
  56.     }  
  57.     public void run()  
  58.     {  
  59.         while(true)  
  60.         {  
  61.             synchronized(r)  
  62.             {  
  63.                 r.out();  
  64.             }  
  65.         }  
  66.     }  
  67. }  
  68. class InputOutputDemo4  
  69. {  
  70.     public static void main(String[] args)  
  71.     {  
  72.         Res r=new Res();  
  73.         new Thread(new Input(r)).start();  
  74.         new Thread(new Output(r)).start();  
  75.         /* 
  76.         Input in=new Input(r); 
  77.         Output out=new Output(r); 
  78.         Thread t1=new Thread(in); 
  79.         Thread t2=new Thread(out); 
  80.         t1.start(); 
  81.         t2.start(); 
  82.         */  
  83.     }  
  84. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值