class SynStack 
{
private char[] data = new char[6];
private int cnt = 0; //表示数组有效元素的个数

public synchronized void push(char ch)
{
while (cnt == data.length)
{
try
{
this.wait(); //将执行this.wait()的当前线程转入堵塞状态,让出CPU的控制权
//释放对this的锁定
}
catch (Exception e)
{
}
}
this.notify();  //执行了此语句后,程序绝对不会立即切换到另一个线程!!

data[cnt] = ch;
++cnt;
System.out.printf("生产线程正在生产第%d个产品,该产品是: %c\n", cnt, ch);
}

public synchronized char pop()
{
char ch;

while (cnt == 0)
{
try
{
this.wait();
}
catch (Exception e)
{
}
}
this.notify();
ch = data[cnt-1];

System.out.printf("消费线程正在消费第%d个产品,该产品是: %c\n", cnt, ch);

--cnt;
return ch;
}
}class Producer implements Runnable
{
private SynStack ss = null;

public Producer(SynStack ss)
{
this.ss = ss;
}

public void run() // throws Exception
{
//push('a');  //error
char ch;

for (int i=0; i<20; ++i)
{
// try{
// Thread.sleep(100);
// }
// catch (Exception e){
// }

ch = (char)('a'+i);
ss.push(ch);
}
}
}class Consumer implements Runnable
{
private SynStack ss = null;

public Consumer(SynStack ss)
{
this.ss = ss;
}

public void run()
{
for (int i=0; i<20; ++i)
{
try{
Thread.sleep(2000);
}
catch (Exception e){
}

//System.out.printf("%c\n", ss.pop());
ss.pop();
}
}
}
public class Test_0
{
public static void main(String[] args)
{
SynStack ss = new SynStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
Thread t1 = new Thread(p);
t1.start();

Thread t2 = new Thread(c);
t2.start();
}
}
1.
          标红色while为什么不能换成if?我试了下,程序的输出结果是一样的,逻辑分析看似也没问题?
2.
           比如当执行完生产者的this.wait()后,while()语句里面的代码还是继续执行?while语句是否属于t1线程内?望大牛能解答,谢谢了~

解决方案 »

  1.   

    这边while可以循环多次,if不行举个例子:
    while(true){
    获取Scanner函数
    }

    if(true){
    获取Scanner函数
    }
    执行获取值,while可以多次获取,if只能一次个人理解两者的区别,不涉及程序
      

  2.   

    问题1:这里的while判断是不能替换成if判断的,原因是可能存在虚假唤醒。程序本身逻辑可能导致虚假唤醒,java虚拟机本身也会因为某些原因而导致线程被虚假唤醒,为了保证程序健壮性,这里的while不能替换成if。
    问题2:调用wait后,调用的线程会被暂停挂起,同时释放锁,这时候如果有另外一个线程持有了锁并执行了notify,被挂起的线程才可能恢复执行。