Virtusa Interview Question

Implement a thread pool with 10 threads in Java?

Interview Answer

Anonymous

Jul 14, 2017

Public class Threadpool{ Public static void main(String args[]) { ExecutorService exe = Executor.newFixedThreadPool(10); For(int i=0; i<=10;i++){ Runnable pool = new WorkerThread(""+i); exe.execute(pool); } exe.shutdown(); while(!exe.isTerminated()){} Sysout(Thread.currentThread.getName()+"finished"); } } class workerThread implements Runnable{ String message; Public workerThread(String message){ Super(); this.message = message; } public void run(){ Syso("start"+ message); try{ Thread.sleep(2000); } catch(Exception e){ e.getMessage(); } Syso("end"); } }

1