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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ylca 中级黑马   /  2016-7-14 18:19  /  946 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public static void main(String[] args) {
  2.                 //创建对象
  3.                 final MyPrinter p = new MyPrinter();
  4.                 //创建线程对象
  5.                 new Thread(){
  6.                         public void run() {
  7.                                 //循环执行
  8.                                 while(true){
  9.                                         try {
  10.                                                 p.print1();
  11.                                         } catch (InterruptedException e) {
  12.                                                 // TODO Auto-generated catch block
  13.                                                 e.printStackTrace();
  14.                                         }
  15.                                 }
  16.                         }
  17.                 }.start();
  18.                 new Thread(){
  19.                         public void run() {
  20.                                 //循环执行
  21.                                 while(true){
  22.                                         try {
  23.                                                 p.print2();
  24.                                         } catch (InterruptedException e) {
  25.                                                 // TODO Auto-generated catch block
  26.                                                 e.printStackTrace();
  27.                                         }
  28.                                 }
  29.                         }
  30.                 }.start();
  31.                 new Thread(){
  32.                         public void run() {
  33.                                 //循环执行
  34.                                 while(true){
  35.                                         try {
  36.                                                 p.print3();
  37.                                         } catch (InterruptedException e) {
  38.                                                 // TODO Auto-generated catch block
  39.                                                 e.printStackTrace();
  40.                                         }
  41.                                 }
  42.                         }
  43.                 }.start();

  44.         }

  45. }
  46. class MyPrinter {
  47.         ReentrantLock r = new ReentrantLock();        //获取互斥锁对象  跟syn同步代码块
  48.         Condition c1 = r.newCondition();                //获取等待跟唤醒功能
  49.         Condition c2 = r.newCondition();
  50.         Condition c3 = r.newCondition();
  51.        
  52.         private int t =1;
  53.         public void print1() throws InterruptedException{
  54.                 r.lock();                //获取锁         跟synchronized 类似       
  55.                 if(t!=1){
  56.                         c1.await();
  57.                 }
  58.                 System.out.println("11111");
  59.                 t=2;
  60.                 c2.signal();        //唤醒线程
  61.                 r.unlock();                //释放锁
  62.         }
  63.        
  64.         public void print2() throws InterruptedException{
  65.                 r.lock();
  66.                 if(t!=2){
  67.                         c2.await();        //线程等待
  68.                 }
  69.                 System.out.println("222222");
  70.                 t=3;
  71.                 c3.signal();        //唤醒线程
  72.                 r.unlock();                //释放锁
  73.         }
  74.        
  75.         public void print3() throws InterruptedException{
  76.                 r.lock();
  77.                 if(t!=3){
  78.                         c3.await();//线程等待
  79.                 }
  80.                 System.out.println("3333333");
  81.                 t=1;
  82.                 c1.signal();        //唤醒线程
  83.                 r.unlock();                //释放锁
  84.         }
  85.        
复制代码


4 个回复

倒序浏览
学习学习
回复 使用道具 举报
线程这一部分的知识点还是挺多的
回复 使用道具 举报
看看  学习学习
回复 使用道具 举报
不错,总结到位
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马