Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.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中带有可运行对象的执行器_Java_Runnable_Executors - Fatal编程技术网

java中带有可运行对象的执行器

java中带有可运行对象的执行器,java,runnable,executors,Java,Runnable,Executors,我当时正试图了解遗嘱执行人的情况,现在有点怀疑 假设我有两个可运行对象Socket1Write和Socket2Write,它们都有一个run方法。运行方法有一个无限循环,将数据写入TCP套接字。套接字是这两个类的通用套接字。但是,当我使用Executor调用两个线程时,只有一个线程被调用和处理,即第一个Runnable总是被拾取 下面是我尝试的方法 ExecutorService executor=Executors.newFixedThreadPool(2); Runnable runner1

我当时正试图了解遗嘱执行人的情况,现在有点怀疑

假设我有两个可运行对象
Socket1Write
Socket2Write
,它们都有一个run方法。运行方法有一个无限循环,将数据写入TCP套接字。套接字是这两个类的通用套接字。但是,当我使用
Executor
调用两个线程时,只有一个线程被调用和处理,即第一个
Runnable
总是被拾取

下面是我尝试的方法

ExecutorService executor=Executors.newFixedThreadPool(2);
Runnable runner1 = new Socket1Write(consumer, socks);
Runnable runner2 = new Socket1Write1(consumer, socks);
executor.execute(runner1);
executor.execute(runner2);

ExecutorService executor=Executors.newFixedThreadPool(1);
ExecutorService executor1=Executors.newFixedThreadPool(1);
Runnable runner1 = new Socket1Write(consumer, socks);
Runnable runner2 = new Socket1Write1(consumer, socks);
executor.execute(runner1);
executor1.execute(runner2);

ExecutorService pool=Executors.newFixedThreadPool(2);
List<Future<Object>> future=new ArrayList<Future<Object>>();

List<Callable<Object>> callList = new ArrayList<Callable<Object>>();
callList.add(new Socket1Write(consumer, socks));
callList.add(new Socket2Write(consumer, socks));
future = pool.invokeAll(callList);
ExecutorService executor=Executors.newFixedThreadPool(2);
Runnable runner1=新Socket1Write(消费者,socks);
Runnable runner2=新的Socket1Write1(消费者,socks);
执行人。执行人(runner1);
执行人。执行人(runner2);
ExecutorService executor=Executors.newFixedThreadPool(1);
ExecutorService executor1=Executors.newFixedThreadPool(1);
Runnable runner1=新Socket1Write(消费者,socks);
Runnable runner2=新的Socket1Write1(消费者,socks);
执行人。执行人(runner1);
执行者1.执行者(runner2);
ExecutorService池=Executors.newFixedThreadPool(2);
List future=new ArrayList();
List callList=new ArrayList();
添加(新Socket1Write(consumer,socks));
添加(新Socket2Write(consumer,socks));
future=pool.invokeAll(调用列表);
根据我的理解,执行者应该选择需要执行的每个线程。我还缺什么东西吗


提前感谢

你说的是哪位遗嘱执行人?为什么有三种方法?@JB Nizet。我用java.util.concurrent.ExecutorService尝试了同样的方法。下面是我尝试的三种不同方法。答案取决于您选择的方法。请专注于一种方法。并发布任务的代码。