今天来学学,你们也好对线程池有一个更好的理解。dom
public class Main { this
public static void main(String[] args) { 线程
Channel channel = new Channel(5); // 工人线程的數量,即线程池内的线程数目 视频
channel.startWorkers();//启动线程池内的线程 队列
new ClientThread("Alice", channel).start();//发送请求的线程,至关于向队列加入请求 资源
new ClientThread("Bobby", channel).start(); get
new ClientThread("Chris", channel).start(); it
} io
} class
发送请求的client代码:
public class ClientThread extends Thread {
private final Channel channel;//至关于线程池
private static final Random random = new Random();
public ClientThread(String name, Channel channel) {
super(name);
this.channel = channel;
}
public void run() {
try {
int i = 0;
Request request = new Request(getName(), i);//生成请求
channel.putRequest(request);//向队列中放入请求,也即把请求传给线程池
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
}
}
}
ClientThread创建请求,并把请求传给了channel,下面来看看channel类(至关于线程池类)
public class Channel {
private static final int MAX_REQUEST = 100;
private final Request[] requestQueue;//存放请求的队列
private int tail; // 下一个putRequest的地方
private int head; // 下一个takeRequest的地方
private int count; // Request的数量
private final WorkerThread[] threadPool;
public Channel(int threads) {
this.requestQueue = new Request[MAX_REQUEST];
this.head = 0;
this.tail = 0;
this.count = 0;
threadPool = new WorkerThread[threads];
for (int i = 0; i < threadPool.length; i++) {
threadPool[i] = new WorkerThread("Worker-" + i, this);//生成线程池中的线程
}
}
public void startWorkers() {
for (int i = 0; i < threadPool.length; i++) {
threadPool[i].start();//启动线程池中的线程
}
}
public synchronized void putRequest(Request request) {//向队列中存入请求
while (count >= requestQueue.length) {
try {
wait();
} catch (InterruptedException e) {
}
}
requestQueue[tail] = request;
tail = (tail + 1) % requestQueue.length;
count++;
notifyAll();
}
public synchronized Request takeRequest() {//从队列取出请求
while (count <= 0) {
try {
wait();
} catch (InterruptedException e) {
}
}
Request request = requestQueue[head];
head = (head + 1) % requestQueue.length;
count--;
notifyAll();
return request;
}
}
channel类把传给他的请求放入队列中,等待worker去取请求,下面看看worker(即工做线程,线程池中已经初始话好的线程)
public class WorkerThread extends Thread {
private final Channel channel;
public WorkerThread(String name, Channel channel) {
super(name);
this.channel = channel;
}
public void run() {
while (true) {
Request request = channel.takeRequest();//取出请求
request.execute();//处理请求
}
}
}
在工做线程中会从线程池的队列里取出请求,并对请求进行处理。这里的workerthread至关于背景线程,他一直都在运行,当有请求的时候,他就会进行处理,这里处理请求的线程是已经存在在channel(线程池里的线程),他不会由于请求的增长而增长(这是本例中的状况),不会来一个请求就新创建一个线程,节省了资源。
再看看请求的代码:
public class Request {
private final String name; // 委托者
private final int number; // 请求编号
private static final Random random = new Random();
public Request(String name, int number) {
this.name = name;
this.number = number;
}
public void execute() {//执行请求
System.out.println(Thread.currentThread().getName() + " executes " + this);
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
}
}
public String toString() {
return "[ Request from " + name + " No." + number + " ]";
}
}
JAVA SDK所写的 ExecutorService,其就至关于channel,即线程池。至于其实现固然要比channel复杂多了,channel只是举个例子。而WorkerThread可不是工做线程,他至关于发送到channel的请求,也就是request,当执行代码:tpes.execute(workers[i]);时,至关于向线程池加入一个请求,而WorkerThread中的run则至关于request中的execute,这也是当执行tpes.execute(workers[i]);时,并不会产生新的线程的缘由。ExecutorService中产生的背景线程(至关于本篇的WorkerThread )咱们是看不到的。
获取各类it视频