Last active
October 6, 2017 12:47
-
-
Save thescouser89/a847b5e15c68810350e9 to your computer and use it in GitHub Desktop.
SimpleThreadPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
class WorkerThread implements Runnable { | |
private String command; | |
public WorkerThread(String s){ | |
this.command = s; | |
} | |
@Override | |
public void run() { | |
System.out.println(Thread.currentThread().getName()+" Start. Command = "+command); | |
processCommand(); | |
System.out.println(Thread.currentThread().getName()+" End."); | |
} | |
private void processCommand() { | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public String toString(){ | |
return this.command; | |
} | |
} | |
public class SimpleThreadPool { | |
public static void main(String[] args) { | |
ExecutorService executor = Executors.newFixedThreadPool(4); | |
for (int i = 0; i < 100; i++) { | |
Runnable worker = new WorkerThread("" + i); | |
executor.execute(worker); | |
} | |
executor.shutdown(); | |
while (!executor.isTerminated()) { | |
} | |
System.out.println("Finished all threads"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment