Multi Thread Java, but only one thread working

谁都会走 提交于 2020-01-01 19:37:28

问题


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);
    }
}

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.
}

And for the result:

Working Thread: Thread-6
Working Thread: Thread-7

Both Thread runs as expected

First 10 seconds:

Bus: Bus 1by Mechanic 1

Another 10 seconds:

Bus: Bus 2by Mechanic 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?

Note: I only use synchronized in the early for wait (for notification), If I don't put it will have network monitoring exception.

EDIT: Added LinkedList and Notify for clarification, all of the List provided is Linked List. an Class.

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();
        }
    }

}

Now they can call and notify now, but the original problem still exists, which is unable to give work to Mechanic2


回答1:


The wait and notify should be performed on the same monotor object.

In your code, you are waiting on

ListBusEngineFix.wait();

while notify by

BusShop.ListBusDepotWaiting.notify();

So the waiting thread will be blocked.



来源:https://stackoverflow.com/questions/49893455/multi-thread-java-but-only-one-thread-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!