JDK线程池:Executors.newFixedThreadPool , Executors.newSingleThreadExecutor,由一个ThreadFactory来创建新的线程,默认情况下为Executors.defaultThreadFactory(),ThreadFactory接口:
public interface ThreadFactory {
Thread newThread(Runnable r);
}
我们可以采用自定义的ThreadFactory工厂,增加对线程创建与销毁等更多的控制,
一个简单的例子,跟踪线程的创建与销毁:
package org.guojje.threadpool;
import java.util.concurrent.atomic.AtomicInteger;
public class WorkThread extends Thread {
private Runnable target;
private AtomicInteger counter;
public WorkThread(Runnable target, AtomicInteger counter) {
this.target = target;
this.counter = counter;
}
@Override
public void run() {
try {
target.run();
} finally {
int c = counter.getAndDecrement();
System.out.println("terminate no " + c + " Threads");
}
}
}
package org.guojje.threadpool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class MyThread implements Runnable {
public static void main(String[] args) {
ExecutorService ctp = Executors.newCachedThreadPool(new ThreadFactory() {
private AtomicInteger count = new AtomicInteger();
public Thread newThread(Runnable r) {
int c = count.incrementAndGet();
System.out.println("create no " + c + " Threads");
return new WorkThread(r,count);
}
});
ctp.execute(new MyThread());
ctp.execute(new MyThread());
ctp.execute(new MyThread());
ctp.execute(new MyThread());
ctp.execute(new MyThread());
ctp.execute(new MyThread());
ctp.shutdown();
try {
ctp.awaitTermination(1200, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run(){
System.out.println("complete a task!!!");
}
}
可以看到在执行这个过程中,共创建过几个线程。
public interface ThreadFactory {
Thread newThread(Runnable r);
}
我们可以采用自定义的ThreadFactory工厂,增加对线程创建与销毁等更多的控制,
一个简单的例子,跟踪线程的创建与销毁:
package org.guojje.threadpool;
import java.util.concurrent.atomic.AtomicInteger;
public class WorkThread extends Thread {
private Runnable target;
private AtomicInteger counter;
public WorkThread(Runnable target, AtomicInteger counter) {
this.target = target;
this.counter = counter;
}
@Override
public void run() {
try {
target.run();
} finally {
int c = counter.getAndDecrement();
System.out.println("terminate no " + c + " Threads");
}
}
}
package org.guojje.threadpool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class MyThread implements Runnable {
public static void main(String[] args) {
ExecutorService ctp = Executors.newCachedThreadPool(new ThreadFactory() {
private AtomicInteger count = new AtomicInteger();
public Thread newThread(Runnable r) {
int c = count.incrementAndGet();
System.out.println("create no " + c + " Threads");
return new WorkThread(r,count);
}
});
ctp.execute(new MyThread());
ctp.execute(new MyThread());
ctp.execute(new MyThread());
ctp.execute(new MyThread());
ctp.execute(new MyThread());
ctp.execute(new MyThread());
ctp.shutdown();
try {
ctp.awaitTermination(1200, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run(){
System.out.println("complete a task!!!");
}
}
可以看到在执行这个过程中,共创建过几个线程。