newCachedThreadPool | 建立可以快取的Thread,每个Thread预设可idle 60秒 |
newFixedThreadPool | 包括固定数量的Thread |
newSingleThreadExecutor | 只有一个Thread,循序的执行指定给它的每个任务 |
newScheduledThreadPool | 可排程的Thread |
newSingleThreadScheduledExecutor | 单一可排程的Thread |
example:
package onlyfun.caterpillar;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExecutorDemo {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(5);
for(int i = 0; i < 10; i++) {
final int count = i;
service.submit(new Runnable() {
public void run() {
System.out.println(count);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
service.shutdown(); // 最后记得关闭Thread pool
}
}
---------------------------------------------------------------------------------------------------------------
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();
}
}
|
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();
}
}
|
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();
}
}
|
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));
}
}
|