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线程内?望大牛能解答,谢谢了~
{
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线程内?望大牛能解答,谢谢了~
解决方案 »
- 为什么我设置布局为null时,面板是一片空白呢?什么组件都没显示。
- GroupLayout问题
- java的sdk中ZipInputStream类的getUTF8String方法
- 50分求教 java菜鸟求救高手推荐一本java入门的好书 最最基础的 没有语言基础
- 如何根据给定参数n一次设定n-1各变量(在线等)
- 大家好,问一个有关JScrollBar的问题
- 在程序运行时,能在某一小段时间内屏蔽掉对JMenu 和 JMenuItem的键盘快捷触发吗?
- 如何把java程序与mysql连接起来
- 快来看看,简单但不容易实现!!!
- 线程同步的输出的问题,在线等
- 在jsp页面 如何在 s:iterator 遍历的时候获取 所遍历的集合长度
- Scanner读取文件问题
while(true){
获取Scanner函数
}
和
if(true){
获取Scanner函数
}
执行获取值,while可以多次获取,if只能一次个人理解两者的区别,不涉及程序
问题2:调用wait后,调用的线程会被暂停挂起,同时释放锁,这时候如果有另外一个线程持有了锁并执行了notify,被挂起的线程才可能恢复执行。