在生产者消费者问题中,线程交流没有发生,代码被卡在某个点
我在Java中做了生产者消费者问题,但是我的代码陷入了system.out.println(“在生产方法中的条件下”)
我认为我的代码不是Abele执行消耗方法
。因此,任何人都可以帮助我解决这个问题,并告诉我这背后的原因以及在代码中应进行哪些修改。
import java.util.LinkedList;
class Consumer extends Thread {
public void run()
{
try{
PC p = PC.getInstance();
p.consume();
Thread.sleep(500);
}
catch(Exception e ){
System.out.println(e);
}
}
}
class Producer extends Thread{
public void run (){
PC p = PC.getInstance();
try{
p.produce();
Thread.sleep(500);
}
catch(Exception e){
System.out.println("interrupted");
}
}
}
class PC {
LinkedList a = new LinkedList();
int capacity = 2;
private static PC single_instance = null;
public static PC getInstance()
{
if (single_instance == null)
single_instance = new PC();
return single_instance;
}
public void consume() throws InterruptedException{
while (true) {
synchronized (this) {
while (a.size() == 0) {
System.out.println("here I am ");
wait();
}
int val = (int) a.removeFirst();
System.out.println("consumer consumed" + val);
notify();
Thread.sleep(500);
}
}
}
public void produce() throws InterruptedException {
while(true) {
int value = 0;
synchronized (this) {
while (a.size() == capacity) {
System.out.println("In condition in produce method " );////
wait();
}
System.out.println("producing value " + value);
a.add(value++);
notify();
System.out.println("after notify in produce method" );
Thread.sleep(500);
}
}
}
}
public class multithreading {
public static void main(String[] args) throws InterruptedException {
Producer p1 = new Producer();
// Producer p2 = new Producer();
Consumer c1 = new Consumer();
p1.start();
c1.start();
p1.join();
c1.join();
}
}
我要输出直到这个
producing value 0
after notify in produce method
producing value 0
after notify in produce method
In condition in produce method
Process finished with exit code 130
I was doing the producer consumer problem in java but my code is getting stuck at System.out.println("In condition in produce method " )
this line.I think due to which my code is not abele to execute the consume method
. So can anyone help me with this and tell me what is the reason behind this and what modifications should be done in the code.
import java.util.LinkedList;
class Consumer extends Thread {
public void run()
{
try{
PC p = PC.getInstance();
p.consume();
Thread.sleep(500);
}
catch(Exception e ){
System.out.println(e);
}
}
}
class Producer extends Thread{
public void run (){
PC p = PC.getInstance();
try{
p.produce();
Thread.sleep(500);
}
catch(Exception e){
System.out.println("interrupted");
}
}
}
class PC {
LinkedList a = new LinkedList();
int capacity = 2;
private static PC single_instance = null;
public static PC getInstance()
{
if (single_instance == null)
single_instance = new PC();
return single_instance;
}
public void consume() throws InterruptedException{
while (true) {
synchronized (this) {
while (a.size() == 0) {
System.out.println("here I am ");
wait();
}
int val = (int) a.removeFirst();
System.out.println("consumer consumed" + val);
notify();
Thread.sleep(500);
}
}
}
public void produce() throws InterruptedException {
while(true) {
int value = 0;
synchronized (this) {
while (a.size() == capacity) {
System.out.println("In condition in produce method " );////
wait();
}
System.out.println("producing value " + value);
a.add(value++);
notify();
System.out.println("after notify in produce method" );
Thread.sleep(500);
}
}
}
}
public class multithreading {
public static void main(String[] args) throws InterruptedException {
Producer p1 = new Producer();
// Producer p2 = new Producer();
Consumer c1 = new Consumer();
p1.start();
c1.start();
p1.join();
c1.join();
}
}
I am getting output till this only
producing value 0
after notify in produce method
producing value 0
after notify in produce method
In condition in produce method
Process finished with exit code 130
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很糟糕:
这很糟糕,因为睡觉的线程没有充分的理由将其他线程锁定在关键部分中。就像,两个室友共享一辆汽车,但其中一位室友拿着钥匙然后上床睡觉。为什么在第一个室友睡觉时,为什么不允许其他室友使用汽车?
这也很糟糕:
这很糟糕,因为离开关键部分后线程所做的下一件事是,它试图重新输入关键部分。 Java的固有锁不是 fair 。刚离开同步块的线程在尝试重新输入时已经运行。另一个线程被阻塞。在这种情况下,操作系统始终会选择已经运行的线程,因为在一个良好的程序中,在一个室友不带车钥匙睡觉的程序中,这通常是这种策略产生最佳性能。
移动
system.out.println()
呼叫和thread.sleep()
呼叫从同步(this)
block中,然后将为另一个线程提供更好的运行机会:@gardener可能已经确定了您的问题:您的单身人士不是线程安全(请参见上面的园丁评论)。您应该在某些静态对象上同步静态getInstance()方法。 (PC类对象将起作用。)
这不是很好:
它不是很好,因为它仅在发生异常时才为您提供最小的信息。这样做是为了在标准错误输出流上获取一条详细的消息,该消息准确地说明了例外情况:
This is bad:
It's bad because the thread that's sleeping has locked other threads out of the critical section for no good reason. It's like, two housemates share a car, but one of the housemates takes the keys and then goes to bed. Why should the other housemate not be allowed to use the car when the first housemate is sleeping?
This also is bad:
It's bad because the very next thing that the thread does after leaving the critical section is, it tries to re-enter the critical section. Java's intrinsic locks are not fair. The thread that just left the synchronized blocks is already running when it tries to re-enter. The other thread is blocked waiting its turn. In that situation, the OS is always going to choose the thread that already is running, because in well-architected program—I.e., in a program where one housemate doesn't go to bed with the car keys—that's the strategy that usually will yield the best performance.
Move the
System.out.println()
calls and theThread.sleep()
calls out of thesynchronized(this)
block, and that will give the other thread a better chance to run:@Gardener may already have identified your problem: Your singleton is not thread-safe (see Gardener's comment, above). You should synchronize the static getInstance() method on some static object. (the PC class object would work.)
This is not great:
It's not great because it gives you only minimal information if an exception occurs. Do this instead to get a detailed message on the standard error output stream that tells exactly where the exception happened: