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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wanghe826 中级黑马   /  2014-5-8 17:22  /  745 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 wanghe826 于 2014-5-8 18:01 编辑

对于多线程的同步我掌握的还不扎实,谁能提供一些代码例子给我看看,如果有注解就更好了,非常感谢!

5 个回复

倒序浏览
  1. /*
  2. 需求:解决通信问题。对资源同步。
  3. */
  4. import java.util.concurrent.locks.*;
  5. class ThreadTest
  6. {
  7.         public static void main(String[] args)
  8.         {
  9.                 Res r = new Res();
  10.                 Output op = new Output(r);
  11.                 Input ip = new Input(r);
  12.                 Thread t1 = new Thread(op);
  13.                 Thread t3 = new Thread(ip);
  14.                 Thread t2 = new Thread(op);
  15.                 Thread t4 = new Thread(ip);

  16.                 t1.start();
  17.                 t2.start();
  18.                 t3.start();
  19.                 t4.start();
  20.         }
  21. }
  22. class Res
  23. {
  24.         private String name;
  25.         private int age;
  26.         boolean flag;
  27.         Lock lock = new ReentrantLock();//拿锁。
  28.         Condition notEmpty = lock.newCondition();//建立condition对象。因为一把锁可以有多个对象。
  29.         Condition notFull = lock.newCondition();
  30.         //这个锁是this锁。
  31.         /*public synchronized void set(String name,int age)
  32.         {
  33.                 while(flag)
  34.                         try
  35.                         {
  36.                                 this.wait();//满足条件线程等待。
  37.                         }
  38.                         catch (Exception e)
  39.                         {
  40.                                 System.out.println(e.toString());
  41.                         }
  42.                 this.name = name;
  43.                 this.age = age;
  44.                 flag = true;//改标记。
  45.                 notifyAll();//唤醒所有线程。
  46.         }
  47.         public synchronized void get()
  48.         {
  49.                 while(!flag)
  50.                         try
  51.                         {
  52.                                 this.wait();//满足条件线程等待。
  53.                         }
  54.                         catch (Exception e)
  55.                         {
  56.                                 System.out.println(e.toString());
  57.                         }
  58.                 System.out.println(this.name  + "" + this.age);
  59.                 flag = false;//改标记
  60.                 notifyAll();//唤醒所有线程。
  61.         }*/
  62.         public void set(String name, int age)
  63.         {
  64.                 lock.lock();//先锁门。
  65.                 try
  66.                 {                       
  67.                         while(flag)
  68.                                 notEmpty.await();//满足条件线程等待。
  69.                         this.name = name;
  70.                         this.age = age;
  71.                         flag = true;//改标记。
  72.                         notFull.signal();//唤醒对方线程。

  73.                 }
  74.                 catch (Exception e)
  75.                 {
  76.                         System.out.println(e.toString());
  77.                 }
  78.                 finally
  79.                 {
  80.                         lock.unlock();//开门。
  81.                 }
  82.         }
  83.         public void get()
  84.         {
  85.                 lock.lock();
  86.                 try
  87.                 {
  88.                         while(!flag)
  89.                                 notFull.await();//满足条件线程等待。
  90.                         System.out.println(Thread.currentThread() + this.name + ":::" + this.age);
  91.                         flag = false;//改标记。
  92.                         notEmpty.signal();//唤醒对方线程
  93.                 }
  94.                 catch (Exception e)
  95.                 {
  96.                         System.out.println(e.toString());
  97.                 }
  98.                 finally
  99.                 {
  100.                         lock.unlock();
  101.                 }
  102.         }       
  103. }
  104. class Input implements Runnable
  105. {
  106.         private Res r;
  107.         Input(Res r)
  108.         {
  109.                 this.r = r;
  110.         }
  111.         public void run()
  112.         {
  113.                 int x = 1;
  114.                 while(true)
  115.                 {
  116.                         if(x == 1)
  117.                                 r.set("mike",34);
  118.                         else
  119.                                 r.set("李丽",25);
  120.                         x = x % 2;
  121.                         x++;               
  122.                 }
  123.         }
  124. }
  125. class Output implements Runnable
  126. {
  127.         private Res r;
  128.         Output(Res r)
  129.         {
  130.                 this.r = r;
  131.         }
  132.         public void run()
  133.         {
  134.                 while (true)
  135.                 {
  136.                         r.get();
  137.                 }
  138.         }
  139. }
复制代码
回复 使用道具 举报

嗯,谢谢!   这个demo不错!
回复 使用道具 举报
刚好也学到这块,学习一下
回复 使用道具 举报
其实拿毕老师的代码和视频多看几遍敲几遍 效果更好哦。
回复 使用道具 举报
伍叶竹 发表于 2014-5-8 19:08
其实拿毕老师的代码和视频多看几遍敲几遍 效果更好哦。

也是额,贵在坚持!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马