Last active
November 28, 2017 09:16
-
-
Save jiunbae/1d3c3ef9bed1eecf49273003d6c07b4b to your computer and use it in GitHub Desktop.
Simple thread pool
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
package concurrent; | |
import java.util.Arrays; | |
import java.util.concurrent.LinkedBlockingQueue; | |
public class Pool { | |
private boolean terminate; | |
private final Worker[] workers; | |
private final LinkedBlockingQueue<Runnable> queue; | |
public Pool(int nThreads) { | |
workers = new Worker[nThreads]; | |
queue = new LinkedBlockingQueue<>(); | |
terminate = false; | |
for (int i = 0; i < nThreads; ++i) { | |
workers[i] = new Worker(); | |
workers[i].start(); | |
} | |
} | |
public void push(Runnable task) { | |
synchronized (queue) { | |
queue.add(task); | |
queue.notify(); | |
} | |
} | |
public void join() { | |
terminate = true; | |
for (Worker worker : workers) | |
try { | |
worker.join(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
public final int size() { | |
return queue.size(); | |
} | |
public final boolean isEmpty() { | |
return queue.isEmpty(); | |
} | |
private class Worker extends Thread { | |
public void run() { | |
Runnable task; | |
while (true) { | |
synchronized (queue) { | |
while (queue.isEmpty()) { | |
try { | |
if (queue.isEmpty() && terminate) | |
return; | |
queue.wait(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
task = queue.poll(); | |
} | |
try { | |
task.run(); | |
} catch (RuntimeException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update join method to terminate after join all workers