一 简介
在JDK1.5以前的线程是没有返回值的(Thread,Runnable),Callable这个接口是之后才出现的新特性,用法跟Runnable类似,只是不同的是可以有返回值。因此为了测试Callable这个类以及线程池相关内容,我将上一篇文章中的代码进行了小幅度的修改然后写了一下
二 关于线程池的简单使用步骤
1 定义线程类,(1)extends Thread (2)implements Runnable (3)implements Callable<>
2 建立ExecutorService线程池,比如这样写:ExecutorService pool = Executors.newCachedThreadPool(); 这是建立一个缓存池。一般常用的还有:(1)newFixedThreadPool() (2)ScheduledThreadPool() (3)SingleThreadExecutor()
3 调用线程池进行操作
//执行任务并获取Future对象
Future<List<String>> future = pool.submit(myThread);
//从Future对象上获取任务的返回值
future.get();
三 完整测试代码
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package
thread;
import
java.io.BufferedWriter;
import
java.io.File;
import
java.io.FileWriter;
import
java.io.IOException;
import
java.util.ArrayList;
import
java.util.Iterator;
import
java.util.List;
import
java.util.concurrent.Callable;
import
java.util.concurrent.ExecutionException;
import
java.util.concurrent.ExecutorService;
import
java.util.concurrent.Executors;
import
java.util.concurrent.Future;
public
class
Test2 {
public
static
void
main(String[] args) {
long
millis1 = System.currentTimeMillis();
String fileName =
"C:\\Users\\Administrator\\Desktop\\测试2.txt"
;
int
threadNum =
5
;
//测试用的线程数目
List<String> list =
new
ArrayList<String>();
//创建一个缓存线程池
ExecutorService pool = Executors.newCachedThreadPool();
for
(
int
i =
0
; i < threadNum; i++) {
Callable<List<String>> myThread =
new
MyThread2(
"线程"
+ i, i, threadNum);
Future<List<String>> future = pool.submit(myThread);
try
{
list.addAll(future.get());
}
catch
(InterruptedException e) {
e.printStackTrace();
}
catch
(ExecutionException e) {
e.printStackTrace();
}
}
pool.shutdown();
//不关闭的话,要等到超时程序才会结束
Iterator<String> iterator = list.iterator();
try
{
BufferedWriter writer =
new
BufferedWriter(
new
FileWriter(
new
File(fileName)));
while
(iterator.hasNext()){
writer.write(iterator.next());
writer.newLine();
writer.flush();
}
writer.close();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long
millis2 = System.currentTimeMillis();
System.out.println(millis2 - millis1);
//大约170-177ms
}
}
/**
* 自定义任务(线程)
* */
class
MyThread2
implements
Callable<List<String>>{
private
String name;
// 线程的名字
private
int
i;
// 第几个线程
private
int
threadNum;
// 总共创建了几个线程
public
MyThread2(String name,
int
i,
int
threadNum) {
this
.name = name;
this
.i = i;
this
.threadNum = threadNum;
}
public
List<String> call()
throws
Exception {
MyPrint2 myPrint2 =
new
MyPrint2();
return
myPrint2.print(name, i, threadNum);
}
}
/**
* 具体的业务操作
* */
class
MyPrint2 {
public
List<String> print(String name,
int
x,
int
threadNum) {
List<String> list =
new
ArrayList<String>();
for
(
int
i = x; i <=
10000
; i = i + threadNum) {
// System.out.println(name + ": " + i + "----------------------------------我是一条华丽的小尾巴");
list.add(name +
": "
+ i +
"----------------------------------我是一条华丽的小尾巴"
);
}
return
list;
}
}
|
注:这种写法由于步骤较多,因此在这里反而比单线程还稍慢O(∩_∩)O~
本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1722426,如需转载请自行联系原作者