Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.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/6/multithreading/4.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 使用AtomicInteger的多线程处理不起作用 公共类示例{ 公共最终原子整数=新原子整数(0); 私有ExecutorService=Executors.newFixedThreadPool(100); 公共无效syn(){ 对于(int i=0;i{ integer.incrementAndGet(); }); } service.shutdown(); System.out.println(integer.get()); } 公共静态void main(字符串[]args){ synchronexample ex=新的synchronexample(); 例如syn(); } }_Java_Multithreading - Fatal编程技术网

Java 使用AtomicInteger的多线程处理不起作用 公共类示例{ 公共最终原子整数=新原子整数(0); 私有ExecutorService=Executors.newFixedThreadPool(100); 公共无效syn(){ 对于(int i=0;i{ integer.incrementAndGet(); }); } service.shutdown(); System.out.println(integer.get()); } 公共静态void main(字符串[]args){ synchronexample ex=新的synchronexample(); 例如syn(); } }

Java 使用AtomicInteger的多线程处理不起作用 公共类示例{ 公共最终原子整数=新原子整数(0); 私有ExecutorService=Executors.newFixedThreadPool(100); 公共无效syn(){ 对于(int i=0;i{ integer.incrementAndGet(); }); } service.shutdown(); System.out.println(integer.get()); } 公共静态void main(字符串[]args){ synchronexample ex=新的synchronexample(); 例如syn(); } },java,multithreading,Java,Multithreading,有人能解释一下为什么这个代码不起作用吗?我的印象是AtomicInteger是线程安全的。并且此代码不会返回1000。AtomicInteger是线程安全的,但在所有任务完成之前,您已经调用了AtomicInteger\get执行器服务#关闭未等待任务完成 见: 此方法不会等待以前提交的任务完成执行。使用此选项可以完成此操作 使用 等待所有任务完成。关闭不是阻塞,它只会阻止将任何新任务添加到服务中。显然,此示例仅用于概念验证,但是否有任何方法仅在所有任务完成后关闭executor服务?而不是硬编

有人能解释一下为什么这个代码不起作用吗?我的印象是AtomicInteger是线程安全的。并且此代码不会返回1000。

AtomicInteger
是线程安全的,但在所有任务完成之前,您已经调用了
AtomicInteger\get
<代码>执行器服务#关闭未等待任务完成

见:

此方法不会等待以前提交的任务完成执行。使用此选项可以完成此操作

使用


等待所有任务完成。

关闭
不是阻塞,它只会阻止将任何新任务添加到服务中。显然,此示例仅用于概念验证,但是否有任何方法仅在所有任务完成后关闭executor服务?而不是硬编码10秒的时间限制?我还注意到,如果我删除service.shutdown(),这个程序仍然会失败(返回错误的号码,可能与任务尚未完成的原因相同),但程序也会永远挂起。这是因为挂起的线程尚未终止吗?当然,这里描述了很多方法:是的,挂起的原因是不间断的线程
shutdown
方法不会中断正在运行的线程,它只指示
ExecutorService
不接受下一个任务<代码>立即关闭中断线程。在
ExecutorService
javadoc中有一个非常好的两阶段关机的例子。
public class SynchroExample {

   public final AtomicInteger integer = new AtomicInteger(0);

   private ExecutorService service = Executors.newFixedThreadPool(100);

   public void syn() {
      for (int i = 0; i < 1000; i++) {
          service.submit(() -> {
              integer.incrementAndGet();
          });
       }
       service.shutdown();
       System.out.println(integer.get());
   }

   public static void main(String [] args) {
      SynchroExample ex = new SynchroExample();
       ex.syn();
    }
}
service.awaitTermination(10, TimeUnit.SECONDS)