package wisers.robot.thread;
import java.util.LinkedList;
import java.util.List;
/ **
* thread pool class , thread manager : create threads , tasks , destroy threads , threads get basic information
* /
public final class ThreadPool { Number
/ / thread pool threads default is 5
private static int worker_num = 3;
/ / worker thread
private WorkThread [] workThrads;
/ / unprocessed task
private static volatile int finished_task = 0;
/ / task queue , as a buffer , List thread safe
private List
private static ThreadPool threadPool;
/ / create a thread pool has a default number of threads
/ * private ThreadPool () {
this (5);
} * /
/ / create a thread pool , worker_num as the number of worker threads in the thread pool
private ThreadPool (int worker_num) {
ThreadPool.worker_num = worker_num;
workThrads = new WorkThread [worker_num];
for (int i = 0; i
workThrads [i]. start () ;/ / open thread pool thread ;
}
}
/ / single- state mode , the default number of threads to get a thread pool
public static ThreadPool getThreadPool () {
return getThreadPool (ThreadPool.worker_num);
}
/ / single- state mode , get a specified number of threads the thread pool , worker_num (> 0) is the number of thread pool threads work
/ / worker_num <= 0 creates a default number of worker threads
public static ThreadPool getThreadPool (int worker_num1) {
if (worker_num1 <= 0)
worker_num1 = ThreadPool.worker_num;
if (threadPool == null)
threadPool = new ThreadPool (worker_num1);
return threadPool;
}
/ / perform a task , the task is only to join the task queue , and when the implementation of a thread pool manager sleep set
public void execute (Runnable task) {
synchronized (taskQueue) {
taskQueue.add (task);
taskQueue.notify ();
}
}
/ / batch tasks , the task is only to join the task queue , and when the implementation of a thread pool manager sleep set
public void execute (Runnable [] task) {
synchronized (taskQueue) {
for (Runnable t: task)
taskQueue.add (t);
taskQueue.notify ();
}
}
/ / batch tasks , the task is only to join the task queue , and when the implementation of a thread pool manager sleep set
public void execute (List
synchronized (taskQueue) {
for (Runnable t: task)
taskQueue.add (t);
taskQueue.notify ();
}
}
/ / destroy the thread pool , this method ensures that in the case of all tasks completed before the destruction of all the threads , or wait until the task is completed destruction
public void destroy () {
while (! taskQueue.isEmpty ()) {/ / If there is no implementation of the task is completed , you will sleep right
try {
Thread.sleep (10000);
} catch (InterruptedException e) {
e.printStackTrace ();
}
}
/ / worker thread to stop working , and is set to null
for (int i = 0; i
workThrads [i] = null;
}
threadPool = null;
taskQueue.clear () ;/ / clear the task queue
}
/ / Returns the number of worker threads
public int getWorkThreadNumber () {
return worker_num;
}
/ / Returns the number of completed tasks , where only completed a number of tasks in the task queue , it may not actually perform the task and completed ;
public int getFinishedTasknumber () {
return finished_task;
}
/ / returns the length of the task queue , that did not deal with the number of tasks
public int getWaitTasknumber () {
return taskQueue.size ();
}
/ / cover toString method returns the thread pool information : Work the number of threads and the number of tasks completed
@ Override
public String toString () {
return "WorkThread number:" + worker_num + "finished task number: "
+ finished_task + " wait task number: "+ getWaitTasknumber ();
}
/ **
* inner classes, the worker thread
* /
private class WorkThread extends Thread {
/ / The worker thread is valid for the end of the working thread
private boolean isRunning = true;
/ *
* key ah , if the task queue is not empty, then remove the task execution , if the task queue is empty , wait
* /
@ Override
public void run () {
Runnable r = null;
while (isRunning) {/ / Note that if the natural end of the thread run method is invalid this thread is useless
synchronized (taskQueue) {
while (isRunning & ; & taskQueue.isEmpty ()) {/ / queue is empty
try {
taskQueue.wait (10000);
} catch (InterruptedException e) {
e.printStackTrace ();
}
}
if (! taskQueue. isEmpty ())
r = taskQueue.remove (0) ;/ / remove the task
}
if (r! = null) {
new Thread (r) start ().;
/ / r.run ();
}
finished_task + +;
r = null;
}
}
/ / stop working , so naturally the thread executing the run method , the natural end
public void stopWorker () {
isRunning = false;
}
}
}
------ Solution ------------------------------------ --------
java.util.concurrent not have it there , so why should write your own ?
challenge themselves what ?
------ Solution ---------------------------------------- ----
executor [ actuators ] to understand what has been perfect thread pool and task management mechanism
------ Solution ------------ --------------------------------
problem:
public void execute (Runnable [] task) {
synchronized (taskQueue) {
for (Runnable t: task)
taskQueue.add (t);
taskQueue.notify ();
}
}
your threads in the pool are synchronized taskQueue, when you add a thread notify only activate one thread , then make the whole thread has been working
Question 2:
ynchronized (taskQueue) {
while (isRunning & ; & taskQueue.isEmpty ()) {/ / queue is empty
try {
taskQueue.wait (10000);
} catch (InterruptedException e) {
e.printStackTrace ();
}
}
if (! taskQueue. isEmpty ())
r = taskQueue.remove (0) ;/ / remove the task
}
if (r! = null) {
new Thread (r) start ().;
you start a new thread in the thread pool thread, the thread pool does not comply with the principle of it , is to use a thread pool thread pool of idle threads to perform tasks to facilitate the management of threads , usually directly in the idle thread perform tasks without the application creates a new thread
Another
Your wording is easy to let the worker thread has been the implementation of the current thread , I suggest you look online sample thread pool areas
------ Solution -------------- ------------------------------
ExecutorService threadPool = Executors.newFixedThreadPool(5);//通过这样可以构造一个线程池,这是一个固定线程数量为5的线程池
threadPool.execut(Runnable task);//这样让线程去执行runnable
------ For reference only - -------------------------------------
may not come into contact with it, limited experience , the Great God , can provide this instance ,
------ For reference only ----------------------------- ----------
Thank you , can you help me to modify my source code , I did not get off before threading code , I hope you can help me
没有评论:
发表评论