2013-05-22 10:05:27
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://computerdragon.blog.51cto.com/6235984/1206497
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package
whut.future;
public
class
FutureMain {
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
System.out.println(
"main BEGIN"
);
Host host=
new
Host();
Data data1=host.request(
10
,
'A'
);
Data data2=host.request(
20
,
'B'
);
System.out.println(
"main otherJob BEGIN"
);
try
{
Thread.sleep(
2000
);
}
catch
(InterruptedException e){
}
System.out.println(
"main otherJob END"
);
System.out.println(
"data1="
+data1.getContent());
System.out.println(
"data2="
+data2.getContent());
System.out.println(
"main END"
);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package
whut.future;
public
class
Host {
//使用Thread-Per-Messagee Pattern
public
Data request(
final
int
count,
final
char
c)
{
System.out.println(
" request("
+count+
","
+c+
") BEGIN"
);
final
FutureData future=
new
FutureData();
//启动线程执行
new
Thread()
{
public
void
run()
{
RealData realData=
new
RealData(count,c);
future.setRealData(realData);
}
}.start();
System.out.println(
" request("
+count+
","
+c+
") END"
);
return
future;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package
whut.future;
//核心部分
public
class
FutureData
implements
Data {
private
RealData realData =
null
;
private
boolean
ready =
false
;
public
synchronized
void
setRealData(RealData realData) {
if
(ready)
return
;
// 利用Balk Pattern来防止赋值多次
this
.realData = realData;
this
.ready =
true
;
notifyAll();
}
public
synchronized
String getContent() {
// 运用了Guarded Suspension Pattern
while
(!ready) {
try
{
wait();
}
catch
(InterruptedException e) {
}
}
return
realData.getContent();
}
}
|
2013-05-22 10:48:38
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://computerdragon.blog.51cto.com/6235984/1206548
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
|
public
class
WorkerTerminalThread
extends
Thread {
// 已经送出终止请求为true,初始化的时候为false
//由于该字段可能会被多个线程访问修改,为了保护就使用这个
private
volatile
boolean
shutdownRequested =
false
;
// 终止请求
public
void
shutdownRequest() {
shutdownRequested =
true
;
interrupt();
}
public
boolean
isShutdownRequest() {
return
shutdownRequested;
}
// 具体动作
public
final
void
run() {
try
{
while
(!shutdownRequested)
doWork();
}
catch
(InterruptedException e) {
}
// 终止处理中的工作,不会进行平常操作,但是会进行终止处理
finally
{
doShutdown();
}
}
// 具体工作操作
private
void
doWork()
throws
InterruptedException {
}
// 终止后进行善后处理
private
void
doShutdown()
{
}
}
|
2013-05-22 11:48:31
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://computerdragon.blog.51cto.com/6235984/1206623
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
|
package
whut.specificstorage;
//负责产生保管箱间的类
public
class
Log {
//首先利用static final修饰
//保证了所有访问该类的线程都只使用一个唯一的且不变的保管箱间。
private
static
final
ThreadLocal tsLogCollections=
new
ThreadLocal();
//不用使用synchronized,因为在使用get和set时候,会字段判别当前所属的线程
//取得当前线程特有的Log
private
static
TSLog getTSLog()
{
TSLog tsLog=(TSLog)tsLogCollections.get();
//如果线程是第一次调用,就建立新文件夹以及注册log
if
(tsLog==
null
)
{
tsLog=
new
TSLog(Thread.currentThread().getName()+
"-log.txt"
);
tsLogCollections.set(tsLog);
}
return
tsLog;
}
//加入一条LOG
public
static
void
println(String s)
{
getTSLog().println(s);
}
//关闭LOG
public
static
void
close()
{
getTSLog().close();
}
}
|