Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android ThreadPoolExecutor和join()_Java_Android_Multithreading_Threadpoolexecutor - Fatal编程技术网

Java Android ThreadPoolExecutor和join()

Java Android ThreadPoolExecutor和join(),java,android,multithreading,threadpoolexecutor,Java,Android,Multithreading,Threadpoolexecutor,我有一个线程池执行器,其中有几个线程同时运行。我想在所有线程完成后执行一些代码。我怎样才能做到这一点 for(String x : mList){ mExecutor.execute(new Runnable(){ @Override public void run() { //Do Stuff } });

我有一个线程池执行器,其中有几个线程同时运行。我想在所有线程完成后执行一些代码。我怎样才能做到这一点

      for(String x : mList){
        mExecutor.execute(new Runnable(){

            @Override
            public void run() {
                //Do Stuff
                            }

        });

       //after all threads finish
       doOtherStuff();

您必须使用
倒计时闩锁
。使用执行器的线程数初始化倒数锁存器。在
Runnable
的末尾,添加
CountDownLatch.countDown()

请注意,调用
await()
将导致调用者阻塞,直到
countDonwL
达到值0。当然,如果在UI线程上调用await(),它将阻塞UI


编辑我在检查语法之前写下了它。因此可能会出现一些打字错误。

您必须使用
倒计时锁存器。使用执行器的线程数初始化倒数锁存器。在
Runnable
的末尾,添加
CountDownLatch.countDown()

请注意,调用
await()
将导致调用者阻塞,直到
countDonwL
达到值0。当然,如果在UI线程上调用await(),它将阻塞UI


编辑我在检查语法之前写下了它。因此可能会出现一些打字错误。

这将满足您的需要:

for(String x : mList) {
   mExecutor.execute(new Runnable() {    
        @Override
        public void run() {
            //Do Stuff
        }    
   });
 }

 mExecutor.shutdown();
 mExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

 //after all threads finish
 doOtherStuff();

这将实现您所需要的:

for(String x : mList) {
   mExecutor.execute(new Runnable() {    
        @Override
        public void run() {
            //Do Stuff
        }    
   });
 }

 mExecutor.shutdown();
 mExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

 //after all threads finish
 doOtherStuff();

使用java.util.concurrency包中提供的CountDownLatch将帮助您实现此处所需的功能

CountDownLatch latch = new CountDownLatch(3);
这一行将创建一个计数为3的倒计时锁存器。

latch.countDown();
latch.await();
每执行一次闩锁,上面这一行将使闩锁减少1。

latch.countDown();
latch.await();

一旦闩锁的倒计时达到0,上面这一行将允许下面代码的执行。在此之后,您可以使用java.util.concurrency包中提供的CountDownLatch来放置需要执行的工作。

将帮助您实现此处所需的功能

CountDownLatch latch = new CountDownLatch(3);
latch.countDown();
latch.await();
这一行将创建一个计数为3的倒计时锁存器。

latch.countDown();
latch.await();
每执行一次闩锁,上面这一行将使闩锁减少1。

latch.countDown();
latch.await();

一旦闩锁的倒计时达到0,上面这一行将允许下面代码的执行。在此之后,您可以放置需要执行的工作……

如果要使用此方法,您应该扩展ThreadPoolExecutor,并将latch.countDown()调用放在重写的afterExecute方法中。我不会把它放在每个runnable中,因为这样做一开始就具有侵入性,而且你必须确保它也在finally块中。但是,我建议使用shutdown()和awaitTermination()使用某人尝试的方法。如果要使用此方法,则应扩展ThreadPoolExecutor,并将latch.countDown()调用放在重写的afterExecute方法中。我不会把它放在每个runnable中,因为这样做一开始就具有侵入性,而且你必须确保它也在finally块中。但是,我推荐某人使用shutdown()和waittermination()的方法。出于兴趣,您选择DAYS有什么原因吗?我会持续几毫秒。@JJG:我想让等待尽可能接近“无限等待”,所以我选择了两个参数的最大值。这只是为了确保你的任务在关卡超时之前无法返回。出于兴趣,你选择了几天有什么原因吗?我会持续几毫秒。@JJG:我想让等待尽可能接近“无限等待”,所以我选择了两个参数的最大值。这只是为了确保在屏障超时之前,您的任务无法返回。
latch.countDown();
latch.await();