Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
b6717d0a
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看板
提交
b6717d0a
编写于
10月 23, 2014
作者:
J
jbachorik
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8058506: ThreadMXBeanStateTest throws exception
Reviewed-by: egahlin, dholmes
上级
efe9dda4
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
152 addition
and
34 deletion
+152
-34
test/java/lang/Thread/ThreadStateController.java
test/java/lang/Thread/ThreadStateController.java
+54
-30
test/java/lang/management/ThreadMXBean/ThreadMXBeanStateTest.java
...a/lang/management/ThreadMXBean/ThreadMXBeanStateTest.java
+8
-4
test/lib/testlibrary/jdk/testlibrary/LockFreeLogManager.java
test/lib/testlibrary/jdk/testlibrary/LockFreeLogManager.java
+90
-0
未找到文件。
test/java/lang/Thread/ThreadStateController.java
浏览文件 @
b6717d0a
/*
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013,
2014
Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -27,6 +27,8 @@ import java.util.concurrent.TimeoutException;
...
@@ -27,6 +27,8 @@ import java.util.concurrent.TimeoutException;
import
java.util.concurrent.atomic.AtomicInteger
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
java.util.concurrent.locks.LockSupport
;
import
java.util.concurrent.locks.LockSupport
;
import
jdk.testlibrary.LockFreeLogManager
;
/**
/**
* ThreadStateController allows a thread to request this thread to transition
* ThreadStateController allows a thread to request this thread to transition
* to a specific thread state. The {@linkplain #transitionTo request} is
* to a specific thread state. The {@linkplain #transitionTo request} is
...
@@ -94,8 +96,12 @@ public class ThreadStateController extends Thread {
...
@@ -94,8 +96,12 @@ public class ThreadStateController extends Thread {
private
static
final
int
S_TERMINATE
=
8
;
private
static
final
int
S_TERMINATE
=
8
;
// for debugging
// for debugging
private
AtomicInteger
iterations
=
new
AtomicInteger
();
private
final
AtomicInteger
iterations
=
new
AtomicInteger
();
private
AtomicInteger
interrupted
=
new
AtomicInteger
();
private
final
AtomicInteger
interrupted
=
new
AtomicInteger
();
private
final
LockFreeLogManager
logManager
=
new
LockFreeLogManager
();
@Override
public
void
run
()
{
public
void
run
()
{
// this thread has started
// this thread has started
while
(!
done
)
{
while
(!
done
)
{
...
@@ -119,13 +125,13 @@ public class ThreadStateController extends Thread {
...
@@ -119,13 +125,13 @@ public class ThreadStateController extends Thread {
break
;
break
;
}
}
case
S_BLOCKED:
{
case
S_BLOCKED:
{
System
.
out
.
format
(
"%d: %s is going to block (in
terations %d)%n"
,
log
(
"%d: %s is going to block (i
terations %d)%n"
,
getId
(),
getName
(),
iterations
.
get
());
getId
(),
getName
(),
iterations
.
get
());
stateChange
(
nextState
);
stateChange
(
nextState
);
// going to block on lock
// going to block on lock
synchronized
(
lock
)
{
synchronized
(
lock
)
{
System
.
out
.
format
(
"%d: %s acquired the lock (in
terations %d)%n"
,
log
(
"%d: %s acquired the lock (i
terations %d)%n"
,
getId
(),
getName
(),
iterations
.
get
());
getId
(),
getName
(),
iterations
.
get
());
try
{
try
{
// this thread has escaped the BLOCKED state
// this thread has escaped the BLOCKED state
// release the lock and a short wait before continue
// release the lock and a short wait before continue
...
@@ -139,13 +145,13 @@ public class ThreadStateController extends Thread {
...
@@ -139,13 +145,13 @@ public class ThreadStateController extends Thread {
}
}
case
S_WAITING:
{
case
S_WAITING:
{
synchronized
(
lock
)
{
synchronized
(
lock
)
{
System
.
out
.
format
(
"%d: %s is going to waiting (in
terations %d interrupted %d)%n"
,
log
(
"%d: %s is going to waiting (i
terations %d interrupted %d)%n"
,
getId
(),
getName
(),
iterations
.
get
(),
interrupted
.
get
());
getId
(),
getName
(),
iterations
.
get
(),
interrupted
.
get
());
try
{
try
{
stateChange
(
nextState
);
stateChange
(
nextState
);
lock
.
wait
();
lock
.
wait
();
System
.
out
.
format
(
"%d: %s wakes up from waiting (in
terations %d interrupted %d)%n"
,
log
(
"%d: %s wakes up from waiting (i
terations %d interrupted %d)%n"
,
getId
(),
getName
(),
iterations
.
get
(),
interrupted
.
get
());
getId
(),
getName
(),
iterations
.
get
(),
interrupted
.
get
());
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
// ignore
// ignore
interrupted
.
incrementAndGet
();
interrupted
.
incrementAndGet
();
...
@@ -155,13 +161,13 @@ public class ThreadStateController extends Thread {
...
@@ -155,13 +161,13 @@ public class ThreadStateController extends Thread {
}
}
case
S_TIMED_WAITING:
{
case
S_TIMED_WAITING:
{
synchronized
(
lock
)
{
synchronized
(
lock
)
{
System
.
out
.
format
(
"%d: %s is going to timed waiting (in
terations %d interrupted %d)%n"
,
log
(
"%d: %s is going to timed waiting (i
terations %d interrupted %d)%n"
,
getId
(),
getName
(),
iterations
.
get
(),
interrupted
.
get
());
getId
(),
getName
(),
iterations
.
get
(),
interrupted
.
get
());
try
{
try
{
stateChange
(
nextState
);
stateChange
(
nextState
);
lock
.
wait
(
10000
);
lock
.
wait
(
10000
);
System
.
out
.
format
(
"%d: %s wakes up from timed waiting (in
terations %d interrupted %d)%n"
,
log
(
"%d: %s wakes up from timed waiting (i
terations %d interrupted %d)%n"
,
getId
(),
getName
(),
iterations
.
get
(),
interrupted
.
get
());
getId
(),
getName
(),
iterations
.
get
(),
interrupted
.
get
());
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
// ignore
// ignore
interrupted
.
incrementAndGet
();
interrupted
.
incrementAndGet
();
...
@@ -170,23 +176,23 @@ public class ThreadStateController extends Thread {
...
@@ -170,23 +176,23 @@ public class ThreadStateController extends Thread {
break
;
break
;
}
}
case
S_PARKED:
{
case
S_PARKED:
{
System
.
out
.
format
(
"%d: %s is going to park (in
terations %d)%n"
,
log
(
"%d: %s is going to park (i
terations %d)%n"
,
getId
(),
getName
(),
iterations
.
get
());
getId
(),
getName
(),
iterations
.
get
());
stateChange
(
nextState
);
stateChange
(
nextState
);
LockSupport
.
park
();
LockSupport
.
park
();
break
;
break
;
}
}
case
S_TIMED_PARKED:
{
case
S_TIMED_PARKED:
{
System
.
out
.
format
(
"%d: %s is going to timed park (in
terations %d)%n"
,
log
(
"%d: %s is going to timed park (i
terations %d)%n"
,
getId
(),
getName
(),
iterations
.
get
());
getId
(),
getName
(),
iterations
.
get
());
long
deadline
=
System
.
currentTimeMillis
()
+
10000
*
1000
;
long
deadline
=
System
.
currentTimeMillis
()
+
10000
*
1000
;
stateChange
(
nextState
);
stateChange
(
nextState
);
LockSupport
.
parkUntil
(
deadline
);
LockSupport
.
parkUntil
(
deadline
);
break
;
break
;
}
}
case
S_SLEEPING:
{
case
S_SLEEPING:
{
System
.
out
.
format
(
"%d: %s is going to sleep (in
terations %d interrupted %d)%n"
,
log
(
"%d: %s is going to sleep (i
terations %d interrupted %d)%n"
,
getId
(),
getName
(),
iterations
.
get
(),
interrupted
.
get
());
getId
(),
getName
(),
iterations
.
get
(),
interrupted
.
get
());
try
{
try
{
stateChange
(
nextState
);
stateChange
(
nextState
);
Thread
.
sleep
(
1000000
);
Thread
.
sleep
(
1000000
);
...
@@ -219,8 +225,8 @@ public class ThreadStateController extends Thread {
...
@@ -219,8 +225,8 @@ public class ThreadStateController extends Thread {
if
(
newState
==
nextState
)
{
if
(
newState
==
nextState
)
{
state
=
nextState
;
state
=
nextState
;
phaser
.
arrive
();
phaser
.
arrive
();
System
.
out
.
format
(
"%d: state change: %s %s%n"
,
log
(
"%d: state change: %s %s%n"
,
getId
(),
toStateName
(
nextState
),
phaserToString
(
phaser
));
getId
(),
toStateName
(
nextState
),
phaserToString
(
phaser
));
return
;
return
;
}
}
...
@@ -270,12 +276,12 @@ public class ThreadStateController extends Thread {
...
@@ -270,12 +276,12 @@ public class ThreadStateController extends Thread {
private
void
nextState
(
int
s
)
throws
InterruptedException
{
private
void
nextState
(
int
s
)
throws
InterruptedException
{
final
long
id
=
Thread
.
currentThread
().
getId
();
final
long
id
=
Thread
.
currentThread
().
getId
();
System
.
out
.
format
(
"%d: wait until the thread transitions to %s %s%n"
,
log
(
"%d: wait until the thread transitions to %s %s%n"
,
id
,
toStateName
(
s
),
phaserToString
(
phaser
));
id
,
toStateName
(
s
),
phaserToString
(
phaser
));
this
.
newState
=
s
;
this
.
newState
=
s
;
int
phase
=
phaser
.
arrive
();
int
phase
=
phaser
.
arrive
();
System
.
out
.
format
(
"%d: awaiting party arrive %s %s%n"
,
log
(
"%d: awaiting party arrive %s %s%n"
,
id
,
toStateName
(
s
),
phaserToString
(
phaser
));
id
,
toStateName
(
s
),
phaserToString
(
phaser
));
for
(;;)
{
for
(;;)
{
// when this thread has changed its state before it waits or parks
// when this thread has changed its state before it waits or parks
// on a lock, a potential race might happen if it misses the notify
// on a lock, a potential race might happen if it misses the notify
...
@@ -301,20 +307,22 @@ public class ThreadStateController extends Thread {
...
@@ -301,20 +307,22 @@ public class ThreadStateController extends Thread {
}
}
try
{
try
{
phaser
.
awaitAdvanceInterruptibly
(
phase
,
100
,
TimeUnit
.
MILLISECONDS
);
phaser
.
awaitAdvanceInterruptibly
(
phase
,
100
,
TimeUnit
.
MILLISECONDS
);
System
.
out
.
format
(
"%d: arrived at %s %s%n"
,
log
(
"%d: arrived at %s %s%n"
,
id
,
toStateName
(
s
),
phaserToString
(
phaser
));
id
,
toStateName
(
s
),
phaserToString
(
phaser
));
return
;
return
;
}
catch
(
TimeoutException
ex
)
{
}
catch
(
TimeoutException
ex
)
{
// this thread hasn't arrived at this phase
// this thread hasn't arrived at this phase
System
.
out
.
format
(
"%d: Timeout: %s%n"
,
id
,
phaser
);
log
(
"%d: Timeout: %s%n"
,
id
,
phaser
);
}
}
}
}
}
}
private
String
phaserToString
(
Phaser
p
)
{
private
String
phaserToString
(
Phaser
p
)
{
return
"[phase = "
+
p
.
getPhase
()
+
return
"[phase = "
+
p
.
getPhase
()
+
" parties = "
+
p
.
getRegisteredParties
()
+
" parties = "
+
p
.
getRegisteredParties
()
+
" arrived = "
+
p
.
getArrivedParties
()
+
"]"
;
" arrived = "
+
p
.
getArrivedParties
()
+
"]"
;
}
}
private
String
toStateName
(
int
state
)
{
private
String
toStateName
(
int
state
)
{
switch
(
state
)
{
switch
(
state
)
{
case
S_RUNNABLE:
case
S_RUNNABLE:
...
@@ -337,4 +345,20 @@ public class ThreadStateController extends Thread {
...
@@ -337,4 +345,20 @@ public class ThreadStateController extends Thread {
return
"unknown "
+
state
;
return
"unknown "
+
state
;
}
}
}
}
private
void
log
(
String
msg
,
Object
...
params
)
{
logManager
.
log
(
msg
,
params
);
}
/**
* Waits for the controller to complete the test run and returns the
* generated log
* @return The controller log
* @throws InterruptedException
*/
public
String
getLog
()
throws
InterruptedException
{
this
.
join
();
return
logManager
.
toString
();
}
}
}
test/java/lang/management/ThreadMXBean/ThreadMXBeanStateTest.java
浏览文件 @
b6717d0a
/*
/*
* Copyright (c) 2003, 201
3
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 201
4
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -31,6 +31,8 @@
...
@@ -31,6 +31,8 @@
* @author Mandy Chung
* @author Mandy Chung
*
*
* @library ../../Thread
* @library ../../Thread
* @library /lib/testlibrary
* @build jdk.testlibrary.LockFreeLogManager
* @build ThreadMXBeanStateTest ThreadStateController
* @build ThreadMXBeanStateTest ThreadStateController
* @run main ThreadMXBeanStateTest
* @run main ThreadMXBeanStateTest
*/
*/
...
@@ -44,15 +46,17 @@ public class ThreadMXBeanStateTest {
...
@@ -44,15 +46,17 @@ public class ThreadMXBeanStateTest {
private
static
final
ThreadMXBean
tm
=
ManagementFactory
.
getThreadMXBean
();
private
static
final
ThreadMXBean
tm
=
ManagementFactory
.
getThreadMXBean
();
static
class
Lock
{
static
class
Lock
{
private
String
name
;
private
final
String
name
;
Lock
(
String
name
)
{
Lock
(
String
name
)
{
this
.
name
=
name
;
this
.
name
=
name
;
}
}
@Override
public
String
toString
()
{
public
String
toString
()
{
return
name
;
return
name
;
}
}
}
}
private
static
Lock
globalLock
=
new
Lock
(
"my lock"
);
private
static
final
Lock
globalLock
=
new
Lock
(
"my lock"
);
public
static
void
main
(
String
[]
argv
)
throws
Exception
{
public
static
void
main
(
String
[]
argv
)
throws
Exception
{
// Force thread state initialization now before the test
// Force thread state initialization now before the test
...
@@ -109,7 +113,7 @@ public class ThreadMXBeanStateTest {
...
@@ -109,7 +113,7 @@ public class ThreadMXBeanStateTest {
thread
.
checkThreadState
(
TERMINATED
);
thread
.
checkThreadState
(
TERMINATED
);
try
{
try
{
thread
.
join
(
);
System
.
out
.
println
(
thread
.
getLog
()
);
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
System
.
out
.
println
(
"TEST FAILED: Unexpected exception."
);
System
.
out
.
println
(
"TEST FAILED: Unexpected exception."
);
...
...
test/lib/testlibrary/jdk/testlibrary/LockFreeLogManager.java
0 → 100644
浏览文件 @
b6717d0a
/*
* Copyright (c) 2014, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package
jdk.testlibrary
;
import
java.util.Collection
;
import
java.util.Formatter
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentLinkedQueue
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
java.util.stream.Collectors
;
/**
* A log manager designed specifically to allow collecting ordered log messages
* in a multi-threaded environment without involving any kind of locking.
* <p>
* It is particularly useful in situations when one needs to assert various
* details about the tested thread state or the locks it hold while also wanting
* to produce diagnostic log messages.
* <p>
* The log manager does not provide any guarantees about the completness of the
* logs written from different threads - it is up to the caller to make sure
* {@code toString()} method is called only when all the activity has ceased
* and the per-thread logs contain all the necessary data.
*
* @author Jaroslav Bachorik
**/
public
class
LockFreeLogManager
{
private
final
AtomicInteger
logCntr
=
new
AtomicInteger
(
0
);
private
final
Collection
<
Map
<
Integer
,
String
>>
allRecords
=
new
ConcurrentLinkedQueue
<>();
private
final
ThreadLocal
<
Map
<
Integer
,
String
>>
records
=
new
ThreadLocal
<
Map
<
Integer
,
String
>>()
{
@Override
protected
Map
<
Integer
,
String
>
initialValue
()
{
Map
<
Integer
,
String
>
m
=
new
ConcurrentHashMap
<>();
allRecords
.
add
(
m
);
return
m
;
}
};
/**
* Log a message
* @param format Message format
* @param params Message parameters
*/
public
void
log
(
String
format
,
Object
...
params
)
{
int
id
=
logCntr
.
getAndIncrement
();
try
(
Formatter
formatter
=
new
Formatter
())
{
records
.
get
().
put
(
id
,
formatter
.
format
(
format
,
params
).
toString
());
}
}
/**
* Will generate an aggregated log of chronologically ordered messages.
* <p>
* Make sure that you call this method only when all the related threads
* have finished; otherwise you might get incomplete data.
*
* @return An aggregated log of chronologically ordered messages
*/
@Override
public
String
toString
()
{
return
allRecords
.
stream
()
.
flatMap
(
m
->
m
.
entrySet
().
stream
())
.
sorted
((
l
,
r
)->
l
.
getKey
().
compareTo
(
r
.
getKey
()))
.
map
(
e
->
e
.
getValue
())
.
collect
(
Collectors
.
joining
());
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录