0

我试图学习 Executors 并且有疑问。

假设我有 2 个 Runnable 对象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);

据我了解,执行者应该选择需要执行的每个线程。我在这里还缺少什么吗?

提前致谢

4

0 回答 0