今天來學學,你們也好對線程池有一個更好的理解。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視頻