带有Runnable对象的java中的执行程序

时间:2017-03-20 05:58:27

标签: java runnable executors

我试图学习执行者并且有疑虑。

假设我有2个具有run方法的Runnable对象Socket1WriteSocket2Write。 Run方法有一个无限循环,它将数据写入TCP Socket。套接字是两个类的通用套接字。但是当我使用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);

从我的理解中,执行程序应该选择需要执行的每个线程。我在这里错过了其他任何东西吗?

提前致谢

0 个答案:

没有答案