Please go through this code and help me to achieve the desired output
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import cyclicbarrier.Hashmap;
public class ThreadpoolDemo {
private static final int nthreads=10;
public static void main(String[] args) throws InterruptedException {
ExecutorService exe=Executors.newFixedThreadPool(nthreads);
for(int i=1;i<7;i++)
{
Runnable worker=new Myrunnable(i);
exe.execute(worker);
}
exe.shutdown();
}
}
class Myrunnable implements Runnable
{
private int ii;
Myrunnable(int i)
{
ii=i;
}
public void run()
{
Hashmap hsamap=new Hashmap();
System.out.println(hsamap.getcity(ii));
System.out.println(Thread.currentThread().getName( )+" Executed this");
Thread.currentThread().stop();
System.out.println(new Date());
}
}
import java.util.HashMap;
public class Hashmap {
public String getcity(int i)
{
HashMap<Integer,String> hm=new HashMap<Integer,String>();
hm.put(1, "AAAA");
hm.put(2, "BBBB");
hm.put(3, "CCCC");
hm.put(4, "DDDD");
hm.put(5, "EEEE");
hm.put(6, "FFFF");
String value=hm.get(i);
return value;
}
}
Output:
BBBB
pool-1-thread-2 Executed this
DDDD
pool-1-thread-4 Executed this
FFFF
pool-1-thread-6 Executed this
AAAA
pool-1-thread-1 Executed this
CCCC
pool-1-thread-3 Executed this
EEEE
pool-1-thread-5 Executed this
now if I change the values to for(int i=3;i<7;i++),I should get the output as
DDDD
pool-1-thread-4 Executed this
EEEE
pool-1-thread-5 Executed this
FFFF
pool-1-thread-6 Executed this
means Thread 1 should always display "AAAA" if i request,Thread 2 should always display "BBBB" and so on Please tell me how can i achieve this. or else if I assign i=3; then thread 3 only should display corresponding Hashmap values,if I assign i=5;then thread 5 only should display corresponding Hashmap values. Thanks in advance