A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

多线程
  1. /*需求:建立两个线程,一个进行输入人的信息,一个负责输出,两个线程交替运行
  2.                 人的信息:name,sex
  3.   分析:1 输入输出的对象均是人
  4.                         故人单独分类,成员变量 :name、sex;
  5.                         同时提供访问和输出成员变量的方法:setInfo out
  6.                         因为setInfo 和 out 两个方法可能被调用同时访问成员变量,因此需要进行同步,
  7.                         只有一个方法访问问成员变量后,才能允许另一个访问。
  8.                 2 输入及输出线程,可通过继承Runnable类,然后覆盖run方法实现
  9.                 3 为确保输入输出线程处理的是同一对象,在创建线程时,应该将对象传递给线程
  10. */
  11. /*版本一 待优化
  12. class Res
  13. {
  14.         private String name;
  15.         private String sex;
  16.         boolean flag = false; //用于标识输出或者输入

  17.         public void setInfo(String name,String sex)
  18.         {
  19.                 this.name = name;
  20.                 this.sex = sex;
  21.         }
  22.         public void out()
  23.         {
  24.                 System.out.println(this.name + "..." + this.sex);
  25.         }
  26. }
  27. class Input implements Runnable
  28. {
  29.         private Res r;        //用于接收操作的对象
  30.         Input(Res r)
  31.         {
  32.                 this.r = r;
  33.         }
  34.         public void run()        //复写run方法,自定义线程的功能:输入信息,交替输入两个人的信息
  35.         {
  36.                 int x = 0;

  37.                 while(true)
  38.                 {
  39.                         synchronized(r)
  40.                         {
  41.                                 if(r.flag)
  42.                                         try{r.wait();}catch (InterruptedException e){}

  43.                                 if(x==0)
  44.                                         r.setInfo("lisi","man");
  45.                                 else
  46.                                         r.setInfo("莉莉","女女");
  47.                                 x = (x+1)%2;

  48.                                 r.flag = (true);        //设置输入完成
  49.                                 r.notify();        //唤醒输出
  50.                         }
  51.                 }
  52.         }
  53. }

  54. class Output implements Runnable
  55. {
  56.         private Res r;        //用于接收操作的对象

  57.         Output(Res r)
  58.         {
  59.                 this.r = r;
  60.         }
  61.         public void run()        //复写run方法,自定义线程的功能:输入信息,交替输入两个人的信息
  62.         {
  63.                 while(true)
  64.                 {
  65.                         synchronized(r)
  66.                         {
  67.                                 if(!r.flag)
  68.                                         try{r.wait();}catch (InterruptedException e){}
  69.                                 r.out();
  70.                                 r.flag = (false);        //设置输出完成标志
  71.                                 r.notify();        //唤醒输入
  72.                         }
  73.                 }
  74.         }
  75. }

  76. */
  77. /*版本二  优化后*/
  78. class Res
  79. {
  80.         private String name;
  81.         private String sex;
  82.         private boolean flag = false; //用于标识输出或者输入

  83.         public synchronized void setInfo(String name,String sex)
  84.         {
  85.                 if(flag)
  86.                         try{this.wait();}catch (InterruptedException e){}  //wait()会抛出异常(函数内,声明后抛或者catch),需要进行处理
  87.                 this.name = name;
  88.                 this.sex = sex;
  89.                 flag = true;        //设置输入完成标识
  90.                 this.notify();        //唤醒输出
  91.         }
  92.         public synchronized void out()
  93.         {
  94.                 if(!flag)
  95.                         try{wait();}catch (InterruptedException e){}
  96.                 System.out.println(this.name + "..." + this.sex);
  97.                 flag = false;        //设置输出完成标识
  98.                 this.notify();        //唤醒输入
  99.         }
  100. }

  101. class Input implements Runnable
  102. {
  103.         private Res r;        //用于接收操作的对象
  104.         Input(Res r)
  105.         {
  106.                 this.r = r;
  107.         }
  108.         public void run()        //复写run方法,自定义线程的功能:输入信息,交替输入两个人的信息
  109.         {
  110.                 int x = 0;
  111.                 while(true)
  112.                 {
  113.                         if(x==0)
  114.                                 r.setInfo("lisi","man");
  115.                         else
  116.                                 r.setInfo("莉莉","女女");
  117.                         x = (x+1)%2;
  118.                 }
  119.         }
  120. }
  121. class Output implements Runnable
  122. {
  123.         private Res r;        //用于接收操作的对象
  124.         Output(Res r)
  125.         {
  126.                 this.r = r;
  127.         }
  128.         public void run()        //复写run方法,自定义线程的功能:输入信息,交替输入两个人的信息
  129.         {
  130.                 while(true)
  131.                 {
  132.                         r.out();
  133.                 }
  134.         }
  135. }
  136. class  InputOutputDemo
  137. {
  138.         public static void main(String[] args)
  139.         {
  140.                 Res r = new Res();
  141.                 new Thread(new Input(r)).start();
  142.                 new Thread(new Output(r)).start();
  143.         }
  144. }



复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马