我有这样一个类:Java并发:一些援助
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.默认情况下他,但看到主要的方法 - 我有2个电话指导方法,其中第二个应该赶上繁忙模式的指导。 2.请参阅编辑的问题。我仍然得到异常 –
你启动了2个线程,但是你可以在任何线程将它们设置为true之前先让这两个线程检查这个标志。 –
@ user3580294但是这并没有用,因为他在方法内部启动另一个线程并运行它。在那个线程里面,他依赖于lockObject。您在该方法上同步只会导致线程的创建和启动被同步。 –