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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杜加璇 中级黑马   /  2013-3-30 20:35  /  1338 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杜加璇 于 2013-3-31 19:10 编辑

class Printer {
private int flag = 1;
public synchronized void print1() throws Exception {   
  if (flag != 1)
   wait();
  System.out.print("传");
  System.out.print("智");
  System.out.print("播");
  System.out.print("客");
  System.out.print("\r\n");
  flag = 2;   
  notifyAll();   
}
public synchronized void print2() throws Exception {
  if (flag != 2)
   wait();
  System.out.print("黑");
  System.out.print("马");
  System.out.print("程");
  System.out.print("序");
  System.out.print("员");
  System.out.print("\r\n");
  flag = 3;   
  notifyAll();   
}
public synchronized void print3() throws Exception {
  if (flag != 3)  
   wait();
  System.out.print("i");
  System.out.print("t");
  System.out.print("c");
  System.out.print("a");
  System.out.print("s");
  System.out.print("t");
  System.out.print("\r\n");
  flag = 1;   
  notifyAll();   
}

}
这个程序为什么用if 是随机唤醒执行而用while是线程1,2,3 顺序唤醒执行呢?
假设用if时 先执行线程1 ,线程1唤醒线程3这时flag是2, 2!=3;线程3执行wait会继续沉睡,直到cpu切换到线程2时才会正常执行 ;同理线程2执行后,直到cpu切换到线程2时才会正常执行
照这个思路 用if时  会是按照线程1,2,3顺序执行的。而用while时也是假设先执行线程1 ,线程1唤醒线程3这时flag是2,那么线程3的while循环将是个死循环 那为什么还会执行呢?
求大神解释。

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1 鼓励鼓励

查看全部评分

4 个回复

倒序浏览

回帖奖励 +1

楼主你好   没明白你的意思   不知道我的代码和你的代码是不是一样的

  1. class Printer
  2. {
  3.         private int flag = 1;
  4.        
  5.                 public synchronized void print1() throws Exception
  6.                 {   
  7.                    while(flag != 1) // 1!=1  FALSE
  8.                    wait();  //  thread_1-->  flag=2
  9.                   
  10.                   System.out.println(Thread.currentThread().getName()+"传智播客");
  11.                   System.out.print("\r\n");
  12.                   flag = 2;   
  13.                   notifyAll();   
  14.                 }
  15.                
  16.                 public synchronized void print2() throws Exception
  17.                 {
  18.                   while(flag != 2)
  19.                    wait();
  20.                   
  21.                   System.out.println(Thread.currentThread().getName()+"黑马程序员");
  22.                   System.out.print("\r\n");
  23.                   flag = 3;   
  24.                   notifyAll();   
  25.                 }

  26.                 public synchronized void print3() throws Exception
  27.                 {
  28.                   while(flag != 3)  
  29.                   wait();
  30.                   
  31.                   System.out.println(Thread.currentThread().getName()+"itcast");
  32.                   System.out.print("\r\n");
  33.                   flag = 1;   
  34.                   this.notifyAll();   
  35.                 }
  36. }

  37. class Temp implements Runnable
  38. {
  39.   Printer p = new Printer();
  40.         public void run()
  41.         {
  42.     try{p.print1();}catch (Exception e) {}
  43.     try{p.print2();}catch (Exception e) {}
  44.     try{p.print3();}catch (Exception e) {}
  45.   }
  46. }

  47. class Run
  48. {
  49.         public static void main(String [] args)
  50.         {
  51.                   Temp t = new Temp();
  52.                         Thread t1 = new Thread(t);
  53.                         Thread t2 = new Thread(t);
  54.                         Thread t3 = new Thread(t);
  55.        
  56.                         t1.start();
  57.                         t2.start();                       
  58.                         t3.start();               
  59.         }
  60. }
复制代码
回复 使用道具 举报
我只是觉得若是这样的代码  用if和while没啥区别啊   LZ把你的全部代码贴上来看看呢
回复 使用道具 举报
你理解错了 。这个问题中if和while没有区别。
回复 使用道具 举报
我来帮你理解一下吧:
假设先执行线程2和3呢?你有没有想过,这样2和3同时等待,执行完1后,是不是就没有连续性可言了呢?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马