Skip to content

Instantly share code, notes, and snippets.

@thescouser89
Last active October 6, 2017 12:47
Show Gist options
  • Save thescouser89/a847b5e15c68810350e9 to your computer and use it in GitHub Desktop.
Save thescouser89/a847b5e15c68810350e9 to your computer and use it in GitHub Desktop.
SimpleThreadPool.java
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