ExecutorService does not block till the thread finishe

时间:2015-05-08 09:54:17

标签: java multithreading threadpool executorservice countdownlatch

i have a class FeatOrientation and in that class in its constructor i perform two operations each one in a separate thread and i am using CountDownLatch so that, when gaussThread finishes the laplaceThread startes.

and in the main class that has the main method i am using ExecutorService and in the ".runAsync(new FeatOrientRun(bgrMat), featOrientExe);"

in the run method i call "calcFeatOrient" function and in that function i instantiate an object from the FeatOrientation class. and the line

Dim Ws As Worksheet
Set Ws =  Workbooks(filename).Sheets(sheetname)

i used it to block the main thread till the two threads in the FeatOrientation class finih and then i do some calculation or display some data as in

With Ws
    Set rLookRange = Range(.Cells(startrow, startcol), .Cells(rows, cols))
End With

the problem is , at run time i receive the output shown below from the console despit the getSmoothedImgList is not empty. I knew that it is not empty by commenting out the following three lines:

"CompletableFuture.allOf(future0).join();" 

and i instantiated an object from the FeatOrientation class in the main method and then , made the main thread to sleep for 7 seconds and after the 7 seconds, i called the following lines

"Log.D(TAG, "MainClass", "SmoothedImgList:" + fo.getSmoothedImgList().size());".

and i received outout.

please let ma know what i am doing wrong with the ExecutorService, and why the approach i am following in the below posted code does not work desoite i wait till the FeatOrientation class finish it works.

console out put:

    featOrientExe = Executors.newFixedThreadPool(1);
    future0 = CompletableFuture.runAsync(new FeatOrientRun(bgrMat), featOrientExe);

CompletableFuture.allOf(future0).join();//blocks the main thread till  future0, and future1 finishes

    featOrientExe.shutdown();

main class:

    Log.D(TAG, "MainClass", "SmoothedImgList:" + fo.getSmoothedImgList().size());
    Log.D(TAG, "MainClass", "SubSampledImgList:" + fo.getSubSampledImgList().size());

    for (int i = 0; i < fo.getSmoothedImgList().size(); i++) {
        ImageUtils.showMat(fo.getSmoothedImgList().get(i), "SmoothedImgList_" + i);
    }
    for (int i = 0; i < fo.getSubSampledImgList().size(); i++) {
        //Mat laplaceImg = SysUtils.applyLaplac(fo.getSubSampledImgList().get(i));
        //ImageUtils.showMat(laplaceImg, "getSubSampledImgList" + i);
    }

FeatOrientation class:

1: Error: FeatOrientation -> getSmoothedImgList: smoothedImgList is empty
Exception in thread "main" java.lang.NullPointerException
at com.example.featorientation_00.MainClass.main(MainClass.java:39)

1 个答案:

答案 0 :(得分:1)

Your collection is not thread safe so while the task may have finished, this doesn't mean the result is visible to another thread.

You could use a thread safe collection, but I prefer to return the results via sha1() and call $client->FetchDiasContactV5( array( 'applicationName' => 'MyVastiauWebApplication' , 'workspace' => '100' , 'addressTable' => 'Client' , 'addressID' => '987654321' ) ); This will both wait for the result and ensure it is passed in a thread safe manner regardless of which collection you use.

I assume this is just an example.
Running threads only makes sense if you have independent tasks (plural) to perform. It makes no sense to start one thread and wait for it's response, you may as well do the work in the current thread as it will be simpler and faster.