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));
}
}
|
本文出自 「i張俊」 博客,出處http://zhangjunhd.blog.51cto.com/113473/70068java