class SimpleThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
return new Thread(r);
}
}
package com.zj.concurrency.executors;
public class MyThread implements Runnable {
private int count = 1, number;
public MyThread(int num) {
number = num;
System.out.println("Create Thread-" + number);
}
public void run() {
while (true) {
System.out.println("Thread-" + number + " run " + count+" time(s)");
if (++count == 3)
return;
}
}
}
package com.zj.concurrency.executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CachedThreadPool {
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++)
exec.execute(new MyThread(i));
exec.shutdown();
}
}
Create Thread-0 Create Thread-1 Create Thread-2 Create Thread-3 Thread-0 run 1 time(s) Thread-0 run 2 time(s) Thread-1 run 1 time(s) Thread-1 run 2 time(s) Thread-2 run 1 time(s) Thread-2 run 2 time(s) Create Thread-4 Thread-4 run 1 time(s) Thread-4 run 2 time(s) Thread-3 run 1 time(s) Thread-3 run 2 time(s)
FixedThreadPool.java
package com.zj.concurrency.executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class FixedThreadPool {
public static void main(String[] args) {
ExecutorService exec = Executors.newFixedThreadPool(2);
for (int i = 0; i < 5; i++)
exec.execute(new MyThread(i));
exec.shutdown();
}
}
Create Thread-0 Create Thread-1 Create Thread-2 Create Thread-3 Create Thread-4 Thread-0 run 1 time(s) Thread-0 run 2 time(s) Thread-2 run 1 time(s) Thread-2 run 2 time(s) Thread-3 run 1 time(s) Thread-3 run 2 time(s) Thread-4 run 1 time(s) Thread-4 run 2 time(s) Thread-1 run 1 time(s) Thread-1 run 2 time(s)
package com.zj.concurrency.executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SingleThreadExecutor {
public static void main(String[] args) {
ExecutorService exec = Executors.newSingleThreadExecutor();
for (int i = 0; i < 5; i++)
exec.execute(new MyThread(i));
exec.shutdown();
}
}
Create Thread-0 Create Thread-1 Create Thread-2 Create Thread-3 Create Thread-4 Thread-0 run 1 time(s) Thread-0 run 2 time(s) Thread-1 run 1 time(s) Thread-1 run 2 time(s) Thread-2 run 1 time(s) Thread-2 run 2 time(s) Thread-3 run 1 time(s) Thread-3 run 2 time(s) Thread-4 run 1 time(s) Thread-4 run 2 time(s)
package com.zj.concurrency.executors.factory;
import java.util.concurrent.ThreadFactory;
public class DaemonThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
}
package com.zj.concurrency.executors.factory;
import java.util.concurrent.ThreadFactory;
public class MaxPriorityThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setPriority(Thread.MAX_PRIORITY);
return t;
}
}
package com.zj.concurrency.executors.factory;
import java.util.concurrent.ThreadFactory;
public class MinPriorityThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setPriority(Thread.MIN_PRIORITY);
return t;
}
}
package com.zj.concurrency.executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.zj.concurrency.executors.factory.DaemonThreadFactory;
import com.zj.concurrency.executors.factory.MaxPriorityThreadFactory;
import com.zj.concurrency.executors.factory.MinPriorityThreadFactory;
public class ExecFromFactory {
public static void main(String[] args) throws Exception {
ExecutorService defaultExec = Executors.newCachedThreadPool();
ExecutorService daemonExec = Executors
.newCachedThreadPool(new DaemonThreadFactory());
ExecutorService maxPriorityExec = Executors
.newCachedThreadPool(new MaxPriorityThreadFactory());
ExecutorService minPriorityExec = Executors
.newCachedThreadPool(new MinPriorityThreadFactory());
for (int i = 0; i < 10; i++)
daemonExec.execute(new MyThread(i));
for (int i = 10; i < 20; i++)
if (i == 10)
maxPriorityExec.execute(new MyThread(i));
else if (i == 11)
minPriorityExec.execute(new MyThread(i));
else
defaultExec.execute(new MyThread(i));
}
}
Create Thread-0 Create Thread-1 Create Thread-2 Create Thread-3 Thread-0 run 1 time(s) Thread-0 run 2 time(s) Thread-1 run 1 time(s) Thread-1 run 2 time(s) Thread-2 run 1 time(s) Thread-2 run 2 time(s) Create Thread-4 Thread-4 run 1 time(s) Thread-4 run 2 time(s) Create Thread-5 Thread-5 run 1 time(s) Thread-5 run 2 time(s) Create Thread-6 Create Thread-7 Thread-7 run 1 time(s) Thread-7 run 2 time(s) Create Thread-8 Thread-8 run 1 time(s) Thread-8 run 2 time(s) Create Thread-9 Create Thread-10 Thread-10 run 1 time(s) Thread-10 run 2 time(s) Create Thread-11 Thread-9 run 1 time(s) Thread-9 run 2 time(s) Thread-6 run 1 time(s) Thread-6 run 2 time(s) Thread-3 run 1 time(s) Thread-3 run 2 time(s) Create Thread-12 Create Thread-13 Create Thread-14 Thread-12 run 1 time(s) Thread-12 run 2 time(s) Thread-13 run 1 time(s) Thread-13 run 2 time(s) Create Thread-15 Thread-15 run 1 time(s) Thread-15 run 2 time(s) Create Thread-16 Thread-16 run 1 time(s) Thread-16 run 2 time(s) Create Thread-17 Create Thread-18 Create Thread-19 Thread-14 run 1 time(s) Thread-14 run 2 time(s) Thread-17 run 1 time(s) Thread-17 run 2 time(s) Thread-18 run 1 time(s) Thread-18 run 2 time(s) Thread-19 run 1 time(s) Thread-19 run 2 time(s) Thread-11 run 1 time(s) Thread-11 run 2 time(s)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有