自己实现的java thread pool
Worker.java
package pool;
public class Worker extends Thread {
private ThreadPool threadPool;
private Runnable task;
public Worker(ThreadPool threadPool) {
this.threadPool = threadPool;
}
public synchronized void push(Runnable task) {
this.task = task;
this.notify();
}
public synchronized void run() {
while(true) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
this.interrupted();
}
if(task == null) {
continue;
}
System.out.println(getName() + "run start");
task.run();
System.out.println(getName() + "run end");
task = null;
threadPool.returnWorker(this);
}
}
}
public class Worker extends Thread {
private ThreadPool threadPool;
private Runnable task;
public Worker(ThreadPool threadPool) {
this.threadPool = threadPool;
}
public synchronized void push(Runnable task) {
this.task = task;
this.notify();
}
public synchronized void run() {
while(true) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
this.interrupted();
}
if(task == null) {
continue;
}
System.out.println(getName() + "run start");
task.run();
System.out.println(getName() + "run end");
task = null;
threadPool.returnWorker(this);
}
}
}
ThreadPool.java
package pool;
import java.util.LinkedList;
import java.util.List;
public class ThreadPool {
private int size;
private List<Worker> idle = new LinkedList<Worker>();
public ThreadPool(int size) {
this.size = size;
for(int i = 0; i < size; i++) {
Worker worker = new Worker(this);
worker.start();
worker.setName ("Worker" + (i + 1));
idle.add(worker);
}
}
synchronized Worker getWorker()
{
Worker worker = null;
if (idle.size() > 0) {
worker = (Worker) idle.remove (0);
} else {
while(true) {
try {
this.wait();
if (idle.size() > 0) {
worker = (Worker) idle.remove (0);
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return (worker);
}
synchronized void returnWorker (Worker worker)
{
idle.add (worker);
this.notifyAll();
}
}
import java.util.LinkedList;
import java.util.List;
public class ThreadPool {
private int size;
private List<Worker> idle = new LinkedList<Worker>();
public ThreadPool(int size) {
this.size = size;
for(int i = 0; i < size; i++) {
Worker worker = new Worker(this);
worker.start();
worker.setName ("Worker" + (i + 1));
idle.add(worker);
}
}
synchronized Worker getWorker()
{
Worker worker = null;
if (idle.size() > 0) {
worker = (Worker) idle.remove (0);
} else {
while(true) {
try {
this.wait();
if (idle.size() > 0) {
worker = (Worker) idle.remove (0);
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return (worker);
}
synchronized void returnWorker (Worker worker)
{
idle.add (worker);
this.notifyAll();
}
}
PrintTask.java
package pool;
public class PrintTask implements Runnable {
private int num;
public PrintTask(int num) {
this.num = num;
}
@Override
public void run() {
System.out.println("num = " + num);
}
}
public class PrintTask implements Runnable {
private int num;
public PrintTask(int num) {
this.num = num;
}
@Override
public void run() {
System.out.println("num = " + num);
}
}
Main.java
package pool;
public class Main {
public static void main(String[] args) {
ThreadPool pool = new ThreadPool(2);
for(int i = 0; i < 1024; i++) {
Worker worker = pool.getWorker();
worker.push(new PrintTask(i));
}
}
}
public class Main {
public static void main(String[] args) {
ThreadPool pool = new ThreadPool(2);
for(int i = 0; i < 1024; i++) {
Worker worker = pool.getWorker();
worker.push(new PrintTask(i));
}
}
}
浙公网安备 33010602011771号