问题描述
My Java Threads不能独立工作,如何解决?这是最初的主要内容:
My Java Threads does not work independently, how to fix it? This is the initial main:
Mechanics mechanics = new Mechanics(busShop, "Mechanic 1");
Mechanics mechanics2 = new Mechanics(busShop, "Mechanic 2");
Thread thMechanic = new Thread(mechanics);
Thread thMehanic2 = new Thread(mechanics2);
thMechanic.start();
thMehanic2.start();
到目前为止没问题,按预期工作,所以机制师这样做:
No problem so far, work as expected, so the mechanics do this:
public void run() {
fixEngine();
}
private void fixEngine() {
while (true) {
busShop.FixEngine(MechanicsName);
}
}
如此处所示它可以永久修复引擎,Inside修复引擎功能:
As seen here it works to fix engine forever, Inside the fix engine function:
public static List<Bus> ListBusEngineFix = new LinkedList();
public void FixEngine(String mechanicsName) {
Bus bus;
synchronized (ListBusEngineFix) {
System.out.println("Working Thread: " + Thread.currentThread().getName());
while (ListBusEngineFix.size() == 0) {
try {
ListBusEngineFix.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//Wait for notify if empty
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//IF EACH THREAD RUNS INDEPENDENTLY IT WILL BT ONLY LESS THAN 20 SECONDS
//BUT THIS CASE, MY PROGRAM WAIT FOR PREVIOUS THREAD, WHICH CAUSE MORE THAN 20 SECONDS.
}
结果如下:
Working Thread: Thread-6
Working Thread: Thread-7
两个线程都按预期运行
前10秒:
Bus: Bus 1by Mechanic 1
另外10秒:
Bus: Bus 2by Mechanic 1
我的怀疑是机械师1(线程)锁定本身,因此机械师2(线程)无法帮助它,因为机械师1完成所有任务。无论如何,如何解决它?
My suspicion would be Mechanic 1 (Thread) lock itself so that Mechanic 2 (Thread) cannot help it, as the Mechanic 1 do all the task. Anyway, how to fix it?
注意:我只在早期使用synchronized进行等待(通知),如果我不说它会有网络监控异常。
Note: I only use synchronized in the early for wait (for notification), If I don't put it will have network monitoring exception.
编辑:添加了LinkedList和Notify以便澄清,所提供的所有列表都是链接列表。一个班级。
Added LinkedList and Notify for clarification, all of the List provided is Linked List. an Class.
编辑2:
好的我已修好如下:
EDIT 2:Ok I have fixed into like this:
synchronized (ListBusDepotWaiting) {
while (ListBusDepotWaiting.size() == 0) {
try {
ListBusDepotWaiting.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//Engine not yet fixed
//Notify the Mechanics to fix car
if (ListBusEngineFix.size() < 2) {
//Means that mechanics is empty
bus = (Bus) ((LinkedList<?>) ListBusDepotWaiting).poll();
((LinkedList<Bus>) ListBusEngineFix).offer(bus);
if (ListBusEngineFix.size() == 1) {
synchronized (ListBusEngineFix) {
ListBusEngineFix.notify();
}
}
}
现在他们现在可以打电话和通知,但原来的问题仍然存在,无法将工作交给 Mechanic2
Now they can call and notify now, but the original problem still exists, which is unable to give work to Mechanic2
推荐答案
应该在同一个monotor对象上执行 wait
和 notify
。
The wait
and notify
should be performed on the same monotor object.
在您的代码中,您正在等待
In your code, you are waiting on
ListBusEngineFix.wait();
而通过
BusShop.ListBusDepotWaiting.notify();
因此等待线程将被阻止。
So the waiting thread will be blocked.
这篇关于多线程Java,但只有一个线程工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!