当前位置:网站首页>Guava multithreading, futurecallback thread calls are uneven

Guava multithreading, futurecallback thread calls are uneven

2022-07-07 20:25:00 zzhongcy

Recent view log , Find out guava The multiple futurecallback Thread calls are uneven , Record it here

Guava Futures brief introduction

Google Guava Framework of the com.google.common.util.concurrent Packages are concurrency related packages , It's right JDK Bring their own
concurrent In bag Future Extensions of classes related to thread pool , Thus, some new classes are derived , And provides a wider range of functions .
The classes commonly used in this package in the project are as follows :

  • ListenableFuture: The interface extends Future Interface , Added addListener Method , This method is used in a given excutor Register a monitor on , When the calculation is completed, the listener will be called immediately . The sequence of listener execution cannot be ensured , But you can ensure that it is called immediately when the calculation is completed .
  • FutureCallback: This interface provides OnSuccess and onFailure Method . Get the result of asynchronous calculation and call back .
  • MoreExecutors: This category is final Tool class of type , Provides a lot of static methods . for example listeningDecorator Method initialization ListeningExecutorService Method , Using this example submit Method to initialize ListenableFuture object .
  • ListenableFutureTask: This class is an adapter , Others can be Future Adaptation ListenableFuture.
  • ListeningExecutorService: This category is right ExecutorService An extension of , Rewrote ExecutorService Class submit Method , And back to ListenableFuture object .
  • JdkFutureAdapters: This class extends FutureTask Class and Implementation ListenableFuture Interface , Added addListener Method .
  • Futures: This class provides many practical static methods for use .

The phenomenon

I created 8 Threads , Average multi task processing , But at the end of the day futurecallback The thread segments called are uneven .

journal :

2022-06-10 16:49:23.402 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=4%
2022-06-10 16:49:23.402 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=8%
2022-06-10 16:49:23.402 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=12%
2022-06-10 16:49:23.407 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=16%
2022-06-10 16:49:23.407 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=20%
2022-06-10 16:49:23.409 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=24%
2022-06-10 16:49:23.409 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=28%
2022-06-10 16:49:23.409 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=32%
2022-06-10 16:49:23.411 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=36%
2022-06-10 16:49:23.411 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=40%
2022-06-10 16:49:23.413 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=44%
2022-06-10 16:49:23.413 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=48%
2022-06-10 16:49:23.413 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=52%
2022-06-10 16:49:23.416 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=56%
2022-06-10 16:49:23.416 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=60%
2022-06-10 16:49:23.418 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=64%
2022-06-10 16:49:23.418 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=68%
2022-06-10 16:49:23.418 [pool-7-thread-7] INFO  c.l.AsyncTask - percent=72%

2022-06-10 16:49:25.231 [pool-7-thread-8] INFO  c.l.AsyncTask - percent=76%
2022-06-10 16:49:27.436 [pool-7-thread-2] INFO  c.l.AsyncTask - percent=80%
2022-06-10 16:49:27.970 [pool-7-thread-4] INFO  c.l.AsyncTask - percent=84%
2022-06-10 16:49:31.812 [pool-7-thread-5] INFO  c.l.AsyncTask - percent=88%
2022-06-10 16:49:33.475 [pool-7-thread-6] INFO  c.l.AsyncTask - percent=92%
2022-06-10 16:49:34.184 [pool-7-thread-3] INFO  c.l.AsyncTask - percent=96%
2022-06-10 16:56:56.214 [pool-7-thread-1] INFO  c.l.AsyncTask - percent=100%

For example :

ExecutorService executorService = Executors.newSingleThreadExecutor(new ThreadFactory() {
    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r);
        t.setDaemon(false);
        return t;
    }
});
ListenableFuture<?> listenableFuture = JdkFutureAdapters.listenInPoolThread(
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("run!!!");
            }
        }),
        executorService
);
Futures.addCallback(listenableFuture, new FutureCallback<Object>() {

    @Override
    public void onSuccess(@Nullable Object result) {
        System.out.println("onSuccess");
    }

    @Override
    public void onFailure(Throwable t) {
        System.out.println("onFailure");
    }
});

Reference resources

java - how Futures.addCallback guarantee the callback to be executed at the thread executing the future task? - Stack Overflow

Futures (Guava: Google Core Libraries for Java 23.0 API)

https://stackoverflow.com/questions/35211331/in-which-thread-futurecallback-will-be-called-for-a-listenablefuture-in-guava

https://stackoverflow.com/questions/63629421/listenable-future-callback-is-heavily-delayed

原网站

版权声明
本文为[zzhongcy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071822085779.html