java.util.concurrent包和Thread-Per-Message模式

Thread-Per-Message模式的七种实现方式

  • java.lang.Thread类

    最基本的创建、启动线程的类

  • java.lang.Runnable接口

    表示线程所执行的“工作”的接口

  • java.util.concurrent.ThreadFactory接口

    将线程创建抽象化了的接口

  • java.util.concurrent.Executor接口

    将线程执行抽象化了的接口

  • java.util.concurrent.ExecutorService接口

    将被复用的线程抽象化了的接口

  • java.util.concurrent.ScheduledExecutorService接口

    将被调度的线程的执行抽象化了的接口

  • java.util.concurrent.Executors类

    用于创建实例的工具类

类图

FbP0ne.png

java.lang.Thread类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Host {
private final Helper helper = new Helper();

public void request(final int count, final char c) {
System.out.println(" request(" + count + ", " + c + ") BEGIN");

new Thread() {
public void run() {
helper.handle(count, c);
}
}.start();

System.out.println(" request(" + count + ", " + c + ") END");
}
}

java.lang.Runnable接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Host {
private final Helper helper = new Helper();

public void request(final int count, final char c) {
System.out.println(" request(" + count + ", " + c + ") BEGIN");

new Thread(
new Runnable() {
public void run() {
helper.handle(count, c);
}
}
).start();

System.out.println(" request(" + count + ", " + c + ") END");
}
}

java.util.concurrent.ThreadFactory接口

java.util.concurrent.ThreadFactory接口声明了如下所示的一个newThread方法。

1
Thread newThread(Runnable r);

ThreadFactory是将线程创建抽象化了的接口,参数中的Runnable对象表示线程执行的操作内容。

Host.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.concurrent.ThreadFactory;

public class Host {
private final Helper helper = new Helper();
private final ThreadFactory threadFactory;

public Host(ThreadFactory threadFactory) {
this.threadFactory = threadFactory;
}

public void request(final int count, final char c) {
System.out.println(" request(" + count + ", " + c + ") BEGIN");

threadFactory.newThread(
new Runnable() {
public void run() {
helper.handle(count, c);
}
}
).start();

System.out.println(" request(" + count + ", " + c + ") END");
}
}

Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.concurrent.ThreadFactory;

public class Main {

public static void main(String[] args) {
System.out.println("main BEGIN");
Host host = new Host(
new ThreadFactory() {

@Override
public Thread newThread(Runnable r) {
return new Thread(r);
}
}
);
host.request(10, 'A');
host.request(20, 'B');
host.request(30, 'C');
System.out.println("main END");
}
}

java.util.concurrent.Executors类获取的ThreadFactory

java.util.concurrent.Executors类提供了许多的静态方法。Executors.defaultThreadFactory()表达式可以获取当前默认设置的ThreadFactory对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.concurrent.Executors;

public class Main {

public static void main(String[] args) {
System.out.println("main BEGIN");
Host host = new Host(
Executors.defaultThreadFactory()
);
host.request(10, 'A');
host.request(20, 'B');
host.request(30, 'C');
System.out.println("main END");
}
}

java.util.concurrent.Executor接口

java.util.concurrent.Executor接口声明了如下所示的一个execute方法。

1
void execute(Runnable r);

Executor接口将某些“处理的执行”抽象化了,参数传入的Runnable对象表示“执行的处理”的内容。

Host.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.concurrent.Executor;

public class Host {
private final Helper helper = new Helper();
private final Executor executor;

public Host(Executor executor) {
this.executor = executor;
}

public void request(final int count, final char c) {
System.out.println(" request(" + count + ", " + c + ") BEGIN");

executor.execute(
new Runnable(){

@Override
public void run() {
helper.handle(count, c);
}
}
);

System.out.println(" request(" + count + ", " + c + ") END");
}
}

Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.concurrent.Executor;

public class Main {

public static void main(String[] args) {
System.out.println("main BEGIN");
Host host = new Host(
new Executor() {
public void execute(Runnable r) {
new Thread(r).start();
}
}
);
host.request(10, 'A');
host.request(20, 'B');
host.request(30, 'C');
System.out.println("main END");
}
}

java.util.concurrent.ExecutorService接口

java.util.concurrent.ExecutorService接口对可以反复execute的服务进行了抽象化。线程一直在后台运行着,每当调用execute方法时,线程就会执行Runnable对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;

public class Main {

public static void main(String[] args) {
System.out.println("main BEGIN");

ExecutorService executorService = Executors.newCachedThreadPool();
Host host = new Host(executorService);

try {
host.request(10, 'A');
host.request(20, 'B');
host.request(30, 'C');
} finally {
executorService.shutdown();
System.out.println("main END");
}
}
}

java.util.concurrent.ScheduledExecutorService接口

java.util.concurrent.ScheduledExecutorService接口是ExecutorService的子接口,用于推迟操作的执行。schedule方法位于ScheduledExecutorService接口中,可以用于设置Runnable对象(r)和延迟时间(delay、unit)。

1
schedule(Runnable r, long delay, TimeUnit unit);

long类型的delay表示的是延迟时间,TimeUnit类型的unit表示的则是指定延迟时间的单位(NANOSECONDS、MICROSECONDS、MILLISECONDS或SECONDS)。

Host.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Host {
private final Helper helper = new Helper();
private final ScheduledExecutorService scheduledExecutorService;

public Host(ScheduledExecutorService scheduledExecutorService) {
this.scheduledExecutorService = scheduledExecutorService;
}

public void request(final int count, final char c) {
System.out.println(" request(" + count + ", " + c + ") BEGIN");

scheduledExecutorService.schedule(
new Runnable(){

@Override
public void run() {
helper.handle(count, c);
}
}
, 3L, TimeUnit.SECONDS
);

System.out.println(" request(" + count + ", " + c + ") END");
}
}

Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class Main {

public static void main(String[] args) {
System.out.println("main BEGIN");

ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
Host host = new Host(scheduledExecutorService);

try {
host.request(10, 'A');
host.request(20, 'B');
host.request(30, 'C');
} finally {
scheduledExecutorService.shutdown();
System.out.println("main END");
}
}
}