gpt4 book ai didi

Java并发: some assistance

转载 作者:太空宇宙 更新时间:2023-11-04 14:52:19 24 4
gpt4 key购买 nike

我有一堂这样的课:

package crossRoadConcurency;

import java.util.List;

public class TourGuide
{
private volatile boolean isGuiding;
private volatile boolean isInShop;
private final Object lockObject = new Object();
private final int id;

public TourGuide(int id)
{
this.isGuiding=false;
this.isInShop=false;
this.id=id;
}

public synchronized boolean isFree()
{
return !isGuiding && !isInShop;
}

public void guide(final Tourist[] tourists)
{
new Thread(new Runnable()
{
@Override
public void run()
{
synchronized (lockObject)
{
while(!isFree())
{
try
{
System.out.println("Guide "+id+" is bussy. Waiting... ");
lockObject.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
isGuiding=true;
System.out.println("Guide "+id+" is guiding "+tourists.length+" tourists");
try
{
Thread.sleep(4000);//lets not wait one hour, shall we?
for (Tourist tourist : tourists)
{
tourist.exit();
}
System.out.println("All tourists exited for guide "+id+". Going to shop");
isInShop=true;
isGuiding=false;//if we invert the way we give these values bad thing may happen
Thread.sleep(4000);
isInShop=false;
System.out.println("Guide "+id+" is free");
synchronized (lockObject)
{
lockObject.notifyAll();
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}

}
}).start();
}
}

还有另一个类:

package crossRoadConcurency;

import java.util.Random;

public class Tourist
{
public void exit() throws InterruptedException
{
Random random = new Random();
Thread.sleep(random.nextInt(1000));// max one second to exit
}
}

我这样使用:

package crossRoadConcurency;

import java.util.List;

public class Main
{

public static void main(String[] args) throws InterruptedException
{
Tourist[] tourists = new Tourist[20];
for (int i=0;i<tourists.length;i++)
{
tourists[i]=new Tourist();
}
TourGuide guide = new TourGuide(0);
guide.guide(tourists);
Thread.sleep(500);
guide.guide(tourists);

}

}

问题是我得到这个输出:

Guide 0 is guiding 20 tourists
All tourists exited for guide 0. Going to shop
Guide 0 is free
Exception in thread "Thread-0" Guide 0 is guiding 20 tourists
java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at crossRoadConcurency.TourGide$1.run(TourGide.java:60)
at java.lang.Thread.run(Unknown Source)
All tourists exited for guide 0. Going to shop
Guide 0 is free
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at crossRoadConcurency.TourGide$1.run(TourGide.java:60)
at java.lang.Thread.run(Unknown Source)

第一个问题:为什么我没有看到“等待输出”
第二个问题:为什么我会遇到异常
第三个问题:有没有更好的方法来做到这一点,因为我相当确定这个概念是混淆的

最佳答案

  1. 您看不到“等待”,因为默认情况下您的指南是免费的。 !isGuide && !isInShop 返回 true,而您仅在 !isFree() 时打印。

  2. 您应该调用lockObject.notifyAll()。默认情况下,直接调用 notifyAll() 会调用此对象,并且您肯定没有持有实现 Runnable 接口(interface)的匿名对象的监视器,因为您从未通过调用 synchronized(this)。这就是您获得异常(exception)的原因。

  3. 是的。您应该使用 Executor 例如SingleThreadExecutor 与并发队列一起。除非您进行高性能计算,否则这是实现并发的强大而简单的方法。该包还提供了出色的功能和支持实用程序类。查看java.util.concurrent

顺便说一句,你的包名包含大写字母,这是java编程规范不推荐的。

关于Java并发: some assistance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23648091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com