【发布时间】:2020-05-08 20:52:55
【问题描述】:
我有以下线程示例:
class Q
{
int num;
public synchronized void put(int num) {
System.out.println("Put :"+num);
this.num = num;
try {Thread.sleep(100);} catch (Exception e) {}
notify();
try {wait();} catch (Exception e) {}
}
public synchronized void get() {
try {wait();} catch (Exception e) {}
System.out.println("Get :"+num);
notify();
}
}
class Producer implements Runnable
{
Q q;
public Producer(Q q) {
this.q = q;
Thread t = new Thread(this,"Producer");
t.start();
}
public void run() {
int i = 0;
while(true) {
q.put(i++);
try {Thread.sleep(1000);} catch (Exception e) {}
}
}
}
class Consumer implements Runnable
{
Q q;
Thread t;
public Consumer(Q q) {
this.q = q;
t = new Thread(this,"Consumer");
t.start();
}
public void run() {
while(true) {
q.get();
try {Thread.sleep(500);} catch (Exception e) {}
}
}
}
public class InterThread {
public static void main(String[] args) {
Q q = new Q();
new Producer(q);
new Consumer(q);
}
}
我试图在一个循环中运行两个线程,消费者和生产者。
共享同一个对象 q,一个线程递增 q.num 并打印它的值,另一个线程通过打印它的值来消耗 q.num。
我在控制台中得到的结果是“Put:0”并停在那里,
即使我使用了Thread.sleep(100);,也没有调用消费者线程
在生产者线程中调用 notify() 之前,为什么!!?
【问题讨论】:
-
你应该注意the documentation of
wait,尤其是这部分:“this method should always be used in a loopwhile (<condition does not hold>) wait();” 你的代码不仅检查失败条件,甚至没有条件检查,因为生产者没有生产任何东西,而消费者没有消费。你所创造的只是某种计数器。 -
是的,我知道这不是使用
wait()的正确方法,我只是想了解这段代码到底在做什么。 -
永远不要从临界区中调用
sleep()(例如,从synchronized块或synchronized方法中,或者,同时保持@987654330 @对象锁定。)唯一的例外是,如果您正在编写一个示例来说明为什么关键部分执行任何需要很长时间的操作是*坏主意*。 -
你为什么要在
put()里面等呢?问你自己。你究竟在等待什么,,当它结束时你打算做什么? A:什么都没有。
标签: java multithreading synchronization wait java-threads