固定线程池
public class ThreadPoolDemo1 {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(5);
for(int i=0;i<6;i++){
service.execute(()->{
try {
TimeUnit.MICROSECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
});
}
System.out.println(service);
service.shutdown();
System.out.println(service);
System.out.println(service.isTerminated());
System.out.println(service.isShutdown());
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(service);
System.out.println(service.isTerminated());
System.out.println(service.isShutdown());
}
}
future
public class FutureDemo {
public static void main(String[] args) {
ExecutorService service= Executors.newFixedThreadPool(5);
Future<Integer> f= service.submit(()->{
TimeUnit.MILLISECONDS.sleep(1);
return 1;
});
try {
System.out.println(f.get());
System.out.println(f.isDone());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
可缓存线程池
public class CachePoolDemo {
public static void main(String[] args) throws InterruptedException {
ExecutorService service= Executors.newCachedThreadPool();
System.out.println(service);
for(int i=0;i<3;i++){
service.submit(()->{
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
});
}
System.out.println(service);
TimeUnit.SECONDS.sleep(60);
System.out.println(service);
TimeUnit.SECONDS.sleep(1);
System.out.println(service);
}
}
单一线程池
public class SingleThreadPool {
public static void main(String[] args) {
ExecutorService service= Executors.newSingleThreadExecutor();
for(int i=0;i<10;i++){
final int j=i;
service.submit(()->{
System.out.println(""+j+Thread.currentThread().getName());
});
}
}
}
可调度线程池
public class ScheduledPoolDemo {
public static void main(String[] args) {
ScheduledExecutorService service= Executors.newScheduledThreadPool(4);
service.scheduleAtFixedRate(()->{
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
},0,500, TimeUnit.MILLISECONDS);
}
}
WorkStealingPool
public class Demo11_WorkStealingPool {
public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newWorkStealingPool();
System.out.println(Runtime.getRuntime().availableProcessors());
service.execute(new R(1000));
service.execute(new R(2000));
service.execute(new R(2000));
service.execute(new R(2000));
service.execute(new R(2000));
service.execute(new R(2000));
service.execute(new R(2000));
service.execute(new R(2000));
service.execute(new R(2000));
service.execute(new R(2000));
service.execute(new R(2000));
service.execute(new R(2000));
service.execute(new R(2000));
TimeUnit.SECONDS.sleep(90);
}
static class R implements Runnable{
int time;
public R (int t){
this.time = t;
}
@Override
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(time + " " + Thread.currentThread().getName());
}
}
}
ForkJoinPool
public class Demo12_ForkJoinPool {
static int[] nums = new int[1000000];
static final int MAX_NUM = 50000;
static Random r = new Random();
static {
for(int i=0; i<nums.length; i++) {
nums[i] = r.nextInt(100);
}
System.out.println(Arrays.stream(nums).sum());
}
static class AddTask extends RecursiveTask<Long> {
int start, end;
AddTask(int s, int e) {
start = s;
end = e;
}
@Override
protected Long compute() {
if(end-start <= MAX_NUM) {
long sum = 0L;
for(int i=start; i<end; i++){
sum += nums[i];
}
return sum;
}
int middle = start + (end-start)/2;
AddTask subTask1 = new AddTask(start, middle);
AddTask subTask2 = new AddTask(middle, end);
subTask1.fork();
subTask2.fork();
return subTask1.join() + subTask2.join();
}
}
public static void main(String[] args) {
ForkJoinPool forkJoinPool = new ForkJoinPool();
AddTask task = new AddTask(0, nums.length);
forkJoinPool.execute(task);
long result = task.join();
System.out.println(result);
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}