Take is cheap, Show me the code.
public class ThreadPoolDemo {
public static void main ( String[ ] args) {
ThreadPool< Job> threadPool = new DefaultThreadPoolDemo < > ( ) ;
IntStream. range ( 0 , 10 ) . forEach ( item - > threadPool. execute ( new Job ( ) ) ) ;
System. out. println ( "Main thread" ) ;
System. out. println ( "JobSize: " + threadPool. getJobSize ( ) ) ;
threadPool. shutdown ( ) ;
}
}
class Job implements Runnable {
@Override
public void run ( ) {
try {
System. out. println ( Thread. currentThread ( ) . getName ( ) + " start..." ) ;
TimeUnit. SECONDS. sleep ( 2 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System. out. println ( Thread. currentThread ( ) . getName ( ) + " end..." ) ;
}
}
interface ThreadPool < Job extends Runnable > {
void execute ( Job job) ;
void shutdown ( ) ;
void addWorkers ( int num) ;
void removeWorkers ( int num) ;
int getJobSize ( ) ;
}
class DefaultThreadPoolDemo < Job extends Runnable > implements ThreadPool < Job> {
private static final int MAX_WORKER_NUMBERS = 10 ;
private static final int DEFAULT_WORKER_NUMBERS = 5 ;
private static final int MIN_WORKERS_NUMBERS = 1 ;
private final LinkedList< Job> jobs = new LinkedList < > ( ) ;
private final List< Worker> workers = Collections. synchronizedList ( new ArrayList < > ( ) ) ;
private int workNum = DEFAULT_WORKER_NUMBERS;
private AtomicLong threadNum = new AtomicLong ( ) ;
public DefaultThreadPoolDemo ( ) {
initialWorkers ( DEFAULT_WORKER_NUMBERS) ;
}
public void DefaultThreadPoolDemo ( int num) {
workNum = num > MAX_WORKER_NUMBERS ? MAX_WORKER_NUMBERS : num < MIN_WORKERS_NUMBERS ? MIN_WORKERS_NUMBERS : num;
initialWorkers ( workNum) ;
}
@Override
public void execute ( Job job) {
if ( job != null) {
synchronized ( jobs) {
jobs. addLast ( job) ;
jobs. notify ( ) ;
}
}
}
@Override
public void shutdown ( ) {
for ( Worker worker : workers) {
worker. shutdown ( ) ;
}
}
@Override
public void addWorkers ( int num) {
synchronized ( jobs) {
int currentWorkerNum = num + this . workNum;
if ( currentWorkerNum > MAX_WORKER_NUMBERS) {
num = MAX_WORKER_NUMBERS - this . workNum;
}
initialWorkers ( num) ;
this . workNum += num;
}
}
@Override
public void removeWorkers ( int num) {
synchronized ( jobs) {
if ( num > this . workNum) {
throw new IllegalStateException ( "超过了当前worker数" ) ;
}
int count = 0 ;
while ( count < num) {
Worker worker = workers. get ( count) ;
if ( workers. remove ( worker) ) {
worker. shutdown ( ) ;
count++ ;
}
}
this . workNum -= count;
}
}
@Override
public int getJobSize ( ) {
return jobs. size ( ) ;
}
private void initialWorkers ( int workNum) {
for ( int i = 0 ; i < workNum; i++ ) {
Worker worker = new Worker ( ) ;
workers. add ( worker) ;
Thread thread = new Thread ( worker, "Thread-pool-worker-" + threadNum) ;
threadNum. getAndIncrement ( ) ;
thread. start ( ) ;
}
}
class Worker implements Runnable {
private volatile boolean running = true ;
@Override
public void run ( ) {
while ( running) {
Job job;
synchronized ( jobs) {
while ( jobs. isEmpty ( ) ) {
try {
jobs. wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
return ;
}
}
job = jobs. removeFirst ( ) ;
}
if ( job != null) {
try {
job. run ( ) ;
} catch ( Exception e) {
}
}
}
}
public void shutdown ( ) {
this . running = false ;
}
}
}