Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
2787ea2f
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
2787ea2f
编写于
3月 10, 2015
作者:
J
jbachorik
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
6712222: Race condition in java/lang/management/ThreadMXBean/AllThreadIds.java
Reviewed-by: dholmes, dfuchs
上级
472c3367
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
49 addition
and
50 deletion
+49
-50
test/java/lang/management/ThreadMXBean/AllThreadIds.java
test/java/lang/management/ThreadMXBean/AllThreadIds.java
+49
-50
未找到文件。
test/java/lang/management/ThreadMXBean/AllThreadIds.java
浏览文件 @
2787ea2f
/*
* Copyright (c) 2003, 201
0
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 201
5
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
...
...
@@ -27,21 +27,19 @@
* @summary Basic unit test of ThreadMXBean.getAllThreadIds()
* @author Alexei Guibadoulline and Mandy Chung
*
* @run build Barrier
* @run main/othervm AllThreadIds
*/
import
java.lang.management.*
;
import
java.util.
*
;
import
java.util.
concurrent.Phaser
;
public
class
AllThreadIds
{
final
static
int
DAEMON_THREADS
=
20
;
final
static
int
USER_THREADS
=
5
;
final
static
int
ALL_THREADS
=
DAEMON_THREADS
+
USER_THREADS
;
private
static
volatile
boolean
live
[]
=
new
boolean
[
ALL_THREADS
];
private
static
Thread
allThreads
[]
=
new
Thread
[
ALL_THREADS
];
private
static
ThreadMXBean
mbean
=
ManagementFactory
.
getThreadMXBean
();
private
static
final
boolean
live
[]
=
new
boolean
[
ALL_THREADS
];
private
static
final
Thread
allThreads
[]
=
new
Thread
[
ALL_THREADS
];
private
static
final
ThreadMXBean
mbean
=
ManagementFactory
.
getThreadMXBean
();
private
static
boolean
testFailed
=
false
;
private
static
boolean
trace
=
false
;
...
...
@@ -52,8 +50,7 @@ public class AllThreadIds {
private
static
int
curLiveThreadCount
=
0
;
private
static
int
curPeakThreadCount
=
0
;
// barrier for threads communication
private
static
Barrier
barrier
=
new
Barrier
(
ALL_THREADS
);
private
static
final
Phaser
startupCheck
=
new
Phaser
(
ALL_THREADS
+
1
);
private
static
void
printThreadList
()
{
if
(!
trace
)
return
;
...
...
@@ -124,18 +121,15 @@ public class AllThreadIds {
curPeakThreadCount
=
mbean
.
getPeakThreadCount
();
checkThreadCount
(
0
,
0
);
// Start all threads and wait to be sure they all are alive
barrier
.
set
(
ALL_THREADS
);
for
(
int
i
=
0
;
i
<
ALL_THREADS
;
i
++)
{
live
[
i
]
=
true
;
setLive
(
i
,
true
)
;
allThreads
[
i
]
=
new
MyThread
(
i
);
allThreads
[
i
].
setDaemon
(
(
i
<
DAEMON_THREADS
)
?
true
:
false
);
allThreads
[
i
].
setDaemon
(
i
<
DAEMON_THREADS
);
allThreads
[
i
].
start
();
}
// wait until all threads are started.
barrier
.
await
();
startupCheck
.
arriveAndAwaitAdvance
();
checkThreadCount
(
ALL_THREADS
,
0
);
printThreadList
();
...
...
@@ -173,15 +167,14 @@ public class AllThreadIds {
// Stop daemon threads, wait to be sure they all are dead, and check
// that they disappeared from getAllThreadIds() list
barrier
.
set
(
DAEMON_THREADS
);
for
(
int
i
=
0
;
i
<
DAEMON_THREADS
;
i
++)
{
live
[
i
]
=
false
;
setLive
(
i
,
false
)
;
}
// wait until daemon threads are terminated.
barrier
.
await
();
// give chance to threads to terminate
pause
();
// make sure the daemon threads are completely dead
joinDaemonThreads
();
// and check the reported thread count
checkThreadCount
(
0
,
DAEMON_THREADS
);
// Check mbean now
...
...
@@ -190,11 +183,11 @@ public class AllThreadIds {
for
(
int
i
=
0
;
i
<
ALL_THREADS
;
i
++)
{
long
expectedId
=
allThreads
[
i
].
getId
();
boolean
found
=
false
;
boolean
live
=
(
i
>=
DAEMON_THREADS
);
boolean
a
live
=
(
i
>=
DAEMON_THREADS
);
if
(
trace
)
{
System
.
out
.
print
(
"Looking for thread with id "
+
expectedId
+
(
live
?
" expected alive."
:
" expected terminated."
));
(
a
live
?
" expected alive."
:
" expected terminated."
));
}
for
(
int
j
=
0
;
j
<
list
.
length
;
j
++)
{
if
(
expectedId
==
list
[
j
])
{
...
...
@@ -203,11 +196,11 @@ public class AllThreadIds {
}
}
if
(
live
!=
found
)
{
if
(
a
live
!=
found
)
{
testFailed
=
true
;
}
if
(
trace
)
{
if
(
live
!=
found
)
{
if
(
a
live
!=
found
)
{
System
.
out
.
println
(
" TEST FAILED."
);
}
else
{
System
.
out
.
println
();
...
...
@@ -216,15 +209,14 @@ public class AllThreadIds {
}
// Stop all threads and wait to be sure they all are dead
barrier
.
set
(
ALL_THREADS
-
DAEMON_THREADS
);
for
(
int
i
=
DAEMON_THREADS
;
i
<
ALL_THREADS
;
i
++)
{
live
[
i
]
=
false
;
setLive
(
i
,
false
)
;
}
// wait until daemon threads are terminated .
barrier
.
await
();
// give chance to threads to terminate
pause
();
// make sure the non-daemon threads are completely dead
joinNonDaemonThreads
();
// and check the thread count
checkThreadCount
(
0
,
ALL_THREADS
-
DAEMON_THREADS
);
if
(
testFailed
)
...
...
@@ -233,6 +225,30 @@ public class AllThreadIds {
System
.
out
.
println
(
"Test passed."
);
}
private
static
void
joinDaemonThreads
()
throws
InterruptedException
{
for
(
int
i
=
0
;
i
<
DAEMON_THREADS
;
i
++)
{
allThreads
[
i
].
join
();
}
}
private
static
void
joinNonDaemonThreads
()
throws
InterruptedException
{
for
(
int
i
=
DAEMON_THREADS
;
i
<
ALL_THREADS
;
i
++)
{
allThreads
[
i
].
join
();
}
}
private
static
void
setLive
(
int
i
,
boolean
val
)
{
synchronized
(
live
)
{
live
[
i
]
=
val
;
}
}
private
static
boolean
isLive
(
int
i
)
{
synchronized
(
live
)
{
return
live
[
i
];
}
}
// The MyThread thread lives as long as correspondent live[i] value is true
private
static
class
MyThread
extends
Thread
{
int
id
;
...
...
@@ -243,8 +259,8 @@ public class AllThreadIds {
public
void
run
()
{
// signal started
barrier
.
signal
();
while
(
live
[
id
]
)
{
startupCheck
.
arrive
();
while
(
isLive
(
id
)
)
{
try
{
sleep
(
100
);
}
catch
(
InterruptedException
e
)
{
...
...
@@ -253,23 +269,6 @@ public class AllThreadIds {
testFailed
=
true
;
}
}
// signal about to exit
barrier
.
signal
();
}
}
private
static
Object
pauseObj
=
new
Object
();
private
static
void
pause
()
{
// Enter lock a without blocking
synchronized
(
pauseObj
)
{
try
{
// may need to tune this timeout for different platforms
pauseObj
.
wait
(
50
);
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
"Unexpected exception."
);
e
.
printStackTrace
(
System
.
err
);
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录