I was trying to learn the Executors and is having doubts.
Assuming that I has 2 Runnable objects Socket1Write and Socket2Write which have a run method. Run methods have an infinite loop which will write data to a TCP Socket. The socket is a common socket for both classes. But when I call both threads using Executor only one is getting called and processed, i.e. the first Runnable is getting picked up always.
Below are the ways I tried.
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);
As from my understanding the executor should pick up each thread that need to be executed. Am I missing any thing else here?
Thanks in advance