Obviously the critical Java section is not working

Here is the code:

final CountDownLatch lineDirectionLatch = new CountDownLatch(count); final Object lock = new Object(); for(StationLines station : stationList) { final String gpsNumber = station.getGpsNumber(); for(String lineNumber : station.getLines()) { final TranslateToStationTask task = new TranslateToStationTask(lineNumber, gpsNumber); task.setCallback(new Runnable() { @Override public void run() { synchronized (lock) { Utils.debug(TAG, "Thead " + Thread.currentThread().getId() + " enter critical section."); int errorCode = task.getTaskResult().getErrorCode(); if (errorCode == 0) { Station station = task.getTaskResult().getContent(); if (station != null) { for(int idx = 0; idx < stationList.size(); idx++) { String gpsNumber = stationList.get(idx).getGpsNumber(); if (gpsNumber.equals(station.getGpsNumber())) { stationList.get(idx).setDirection(station.getLineNumber(), station.getDirection()); } } } } Utils.debug(TAG, "Thead " + Thread.currentThread().getId() + " leave critical section."); lineDirectionLatch.countDown(); } }}); task.startTask(); } } lineDirectionLatch.await(); 

Herer is the journal:

 03-26 19:57:07.648: I/TrafficManager(20013): Thead 1501 enter critical section. 03-26 19:57:07.687: I/TrafficManager(20013): Thead 1501 leave critical section. 03-26 19:57:07.714: I/TrafficManager(20013): Thead 1502 enter critical section. 03-26 19:57:07.722: I/TrafficManager(20013): Thead 1502 leave critical section. 03-26 19:57:07.726: I/TrafficManager(20013): Thead 1503 enter critical section. 03-26 19:57:07.726: I/TrafficManager(20013): Thead 1503 leave critical section. 03-26 19:57:07.769: I/TrafficManager(20013): Thead 1509 enter critical section. 03-26 19:57:07.773: I/TrafficManager(20013): Thead 1505 enter critical section. 03-26 19:57:07.773: I/TrafficManager(20013): Thead 1505 leave critical section. 03-26 19:57:07.804: I/TrafficManager(20013): Thead 1506 enter critical section. 03-26 19:57:07.804: I/TrafficManager(20013): Thead 1506 leave critical section. 03-26 19:57:07.804: I/TrafficManager(20013): Thead 1507 enter critical section. 03-26 19:57:07.804: I/TrafficManager(20013): Thead 1507 leave critical section. 03-26 19:57:07.804: I/TrafficManager(20013): Thead 1510 enter critical section. 03-26 19:57:07.843: I/TrafficManager(20013): Thead 1511 enter critical section. 03-26 19:57:07.843: I/TrafficManager(20013): Thead 1513 enter critical section. 

You can see that Thread 1505 enters the critical section from the previously entered thread (1509), and thread 1509 never leaves the critical section? This is strange.

Each TranslateToStationTask starts in its thread, and the critical section will be called when the task completes.

I have no idea why the critical section allows you to enter multiple tasks and why some tasks seem never to leave the critical section?

+4
source share
3 answers

Please add a try-catch clause inside the synchronized block to see if the function exits abnormally and therefore unlock the object without notification.

Something like that:

 synchronized(lock) { try { // my code } catch (Exception e) { // generic catch to look if any exception is triggered System.out.printf("Caused exception: %s",e.getMessage()); } } 

Another possible problem is that you are creating an anonymous class with a new Runnable (), so it is possible that the lock is not always the same variable. Try creating a class that implements Runnable along with a lock variable, and call this constructor.

+2
source

From your code, it looks like you are creating a lock object in the function itself. Therefore, every time a function is called, there will be a new lock for different threads.

Therefore, all of them will be included in your critical section.

Derive it from this function to get the desired behavior.

+4
source

Thread 1509 does not exit the critical section anywhere in the journal. Perhaps it is waiting for a lock , which will allow other threads to enter the critical section? (It is unlikely since lock does not seem to be passed anywhere outside the method in the code snippet).

0
source

All Articles