Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
aa86f2c8
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看板
提交
aa86f2c8
编写于
7月 03, 2013
作者:
P
psandoz
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8019481: Sync misc j.u.c classes from 166 to tl
Reviewed-by: martin Contributed-by:
N
Doug Lea
<
dl@cs.oswego.edu
>
上级
7bf1893e
变更
8
展开全部
隐藏空白更改
内联
并排
Showing
8 changed file
with
584 addition
and
596 deletion
+584
-596
src/share/classes/java/util/concurrent/BrokenBarrierException.java
.../classes/java/util/concurrent/BrokenBarrierException.java
+2
-2
src/share/classes/java/util/concurrent/CountDownLatch.java
src/share/classes/java/util/concurrent/CountDownLatch.java
+13
-13
src/share/classes/java/util/concurrent/CyclicBarrier.java
src/share/classes/java/util/concurrent/CyclicBarrier.java
+32
-27
src/share/classes/java/util/concurrent/Exchanger.java
src/share/classes/java/util/concurrent/Exchanger.java
+404
-431
src/share/classes/java/util/concurrent/Phaser.java
src/share/classes/java/util/concurrent/Phaser.java
+76
-67
src/share/classes/java/util/concurrent/TimeUnit.java
src/share/classes/java/util/concurrent/TimeUnit.java
+46
-47
src/share/classes/java/util/concurrent/TimeoutException.java
src/share/classes/java/util/concurrent/TimeoutException.java
+3
-3
src/share/classes/java/util/concurrent/package-info.java
src/share/classes/java/util/concurrent/package-info.java
+8
-6
未找到文件。
src/share/classes/java/util/concurrent/BrokenBarrierException.java
浏览文件 @
aa86f2c8
...
...
@@ -49,13 +49,13 @@ public class BrokenBarrierException extends Exception {
private
static
final
long
serialVersionUID
=
7117394618823254244L
;
/**
* Constructs a
<tt>BrokenBarrierException</tt>
with no specified detail
* Constructs a
{@code BrokenBarrierException}
with no specified detail
* message.
*/
public
BrokenBarrierException
()
{}
/**
* Constructs a
<tt>BrokenBarrierException</tt>
with the specified
* Constructs a
{@code BrokenBarrierException}
with the specified
* detail message.
*
* @param message the detail message
...
...
src/share/classes/java/util/concurrent/CountDownLatch.java
浏览文件 @
aa86f2c8
...
...
@@ -92,15 +92,15 @@ import java.util.concurrent.locks.AbstractQueuedSynchronizer;
* private final CountDownLatch startSignal;
* private final CountDownLatch doneSignal;
* Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
*
this.startSignal = startSignal;
*
this.doneSignal = doneSignal;
* this.startSignal = startSignal;
* this.doneSignal = doneSignal;
* }
* public void run() {
*
try {
*
startSignal.await();
*
doWork();
*
doneSignal.countDown();
*
} catch (InterruptedException ex) {} // return;
* try {
* startSignal.await();
* doWork();
* doneSignal.countDown();
* } catch (InterruptedException ex) {} // return;
* }
*
* void doWork() { ... }
...
...
@@ -130,14 +130,14 @@ import java.util.concurrent.locks.AbstractQueuedSynchronizer;
* private final CountDownLatch doneSignal;
* private final int i;
* WorkerRunnable(CountDownLatch doneSignal, int i) {
*
this.doneSignal = doneSignal;
*
this.i = i;
* this.doneSignal = doneSignal;
* this.i = i;
* }
* public void run() {
*
try {
*
doWork(i);
*
doneSignal.countDown();
*
} catch (InterruptedException ex) {} // return;
* try {
* doWork(i);
* doneSignal.countDown();
* } catch (InterruptedException ex) {} // return;
* }
*
* void doWork() { ... }
...
...
src/share/classes/java/util/concurrent/CyclicBarrier.java
浏览文件 @
aa86f2c8
...
...
@@ -45,14 +45,14 @@ import java.util.concurrent.locks.ReentrantLock;
* <em>cyclic</em> because it can be re-used after the waiting threads
* are released.
*
* <p>A
<tt>CyclicBarrier</tt>
supports an optional {@link Runnable} command
* <p>A
{@code CyclicBarrier}
supports an optional {@link Runnable} command
* that is run once per barrier point, after the last thread in the party
* arrives, but before any threads are released.
* This <em>barrier action</em> is useful
* for updating shared-state before any of the parties continue.
*
* <p><b>Sample usage:</b> Here is an example of
*
using a barrier in a
parallel decomposition design:
* <p><b>Sample usage:</b> Here is an example of
using a barrier in a
* parallel decomposition design:
*
* <pre> {@code
* class Solver {
...
...
@@ -81,16 +81,20 @@ import java.util.concurrent.locks.ReentrantLock;
* public Solver(float[][] matrix) {
* data = matrix;
* N = matrix.length;
* barrier = new CyclicBarrier(N,
* new Runnable() {
* public void run() {
* mergeRows(...);
* }
* });
* for (int i = 0; i < N; ++i)
* new Thread(new Worker(i)).start();
* Runnable barrierAction =
* new Runnable() { public void run() { mergeRows(...); }};
* barrier = new CyclicBarrier(N, barrierAction);
*
* waitUntilDone();
* List<Thread> threads = new ArrayList<Thread>(N);
* for (int i = 0; i < N; i++) {
* Thread thread = new Thread(new Worker(i));
* threads.add(thread);
* thread.start();
* }
*
* // wait until done
* for (Thread thread : threads)
* thread.join();
* }
* }}</pre>
*
...
...
@@ -98,8 +102,8 @@ import java.util.concurrent.locks.ReentrantLock;
* barrier until all rows have been processed. When all rows are processed
* the supplied {@link Runnable} barrier action is executed and merges the
* rows. If the merger
* determines that a solution has been found then
<tt>done()</tt>
will return
*
<tt>true</tt>
and each worker will terminate.
* determines that a solution has been found then
{@code done()}
will return
*
{@code true}
and each worker will terminate.
*
* <p>If the barrier action does not rely on the parties being suspended when
* it is executed, then any of the threads in the party could execute that
...
...
@@ -112,7 +116,7 @@ import java.util.concurrent.locks.ReentrantLock;
* // log the completion of this iteration
* }}</pre>
*
* <p>The
<tt>CyclicBarrier</tt>
uses an all-or-none breakage model
* <p>The
{@code CyclicBarrier}
uses an all-or-none breakage model
* for failed synchronization attempts: If a thread leaves a barrier
* point prematurely because of interruption, failure, or timeout, all
* other threads waiting at that barrier point will also leave
...
...
@@ -139,7 +143,7 @@ public class CyclicBarrier {
* is reset. There can be many generations associated with threads
* using the barrier - due to the non-deterministic way the lock
* may be allocated to waiting threads - but only one of these
* can be active at a time (the one to which
<tt>count</tt>
applies)
* can be active at a time (the one to which
{@code count}
applies)
* and all the rest are either broken or tripped.
* There need not be an active generation if there has been a break
* but no subsequent reset.
...
...
@@ -259,7 +263,7 @@ public class CyclicBarrier {
}
/**
* Creates a new
<tt>CyclicBarrier</tt>
that will trip when the
* Creates a new
{@code CyclicBarrier}
that will trip when the
* given number of parties (threads) are waiting upon it, and which
* will execute the given barrier action when the barrier is tripped,
* performed by the last thread entering the barrier.
...
...
@@ -278,7 +282,7 @@ public class CyclicBarrier {
}
/**
* Creates a new
<tt>CyclicBarrier</tt>
that will trip when the
* Creates a new
{@code CyclicBarrier}
that will trip when the
* given number of parties (threads) are waiting upon it, and
* does not perform a predefined action when the barrier is tripped.
*
...
...
@@ -301,7 +305,7 @@ public class CyclicBarrier {
/**
* Waits until all {@linkplain #getParties parties} have invoked
*
<tt>await</tt>
on this barrier.
*
{@code await}
on this barrier.
*
* <p>If the current thread is not the last to arrive then it is
* disabled for thread scheduling purposes and lies dormant until
...
...
@@ -326,7 +330,7 @@ public class CyclicBarrier {
*
* <p>If the barrier is {@link #reset} while any thread is waiting,
* or if the barrier {@linkplain #isBroken is broken} when
*
<tt>await</tt>
is invoked, or while any thread is waiting, then
*
{@code await}
is invoked, or while any thread is waiting, then
* {@link BrokenBarrierException} is thrown.
*
* <p>If any thread is {@linkplain Thread#interrupt interrupted} while waiting,
...
...
@@ -343,7 +347,7 @@ public class CyclicBarrier {
* the broken state.
*
* @return the arrival index of the current thread, where index
*
<tt>{@link #getParties()} - 1</tt>
indicates the first
*
{@code getParties() - 1}
indicates the first
* to arrive and zero indicates the last to arrive
* @throws InterruptedException if the current thread was interrupted
* while waiting
...
...
@@ -351,7 +355,7 @@ public class CyclicBarrier {
* interrupted or timed out while the current thread was
* waiting, or the barrier was reset, or the barrier was
* broken when {@code await} was called, or the barrier
* action (if present) failed due
an exception.
* action (if present) failed due
to an exception
*/
public
int
await
()
throws
InterruptedException
,
BrokenBarrierException
{
try
{
...
...
@@ -363,7 +367,7 @@ public class CyclicBarrier {
/**
* Waits until all {@linkplain #getParties parties} have invoked
*
<tt>await</tt>
on this barrier, or the specified waiting time elapses.
*
{@code await}
on this barrier, or the specified waiting time elapses.
*
* <p>If the current thread is not the last to arrive then it is
* disabled for thread scheduling purposes and lies dormant until
...
...
@@ -393,7 +397,7 @@ public class CyclicBarrier {
*
* <p>If the barrier is {@link #reset} while any thread is waiting,
* or if the barrier {@linkplain #isBroken is broken} when
*
<tt>await</tt>
is invoked, or while any thread is waiting, then
*
{@code await}
is invoked, or while any thread is waiting, then
* {@link BrokenBarrierException} is thrown.
*
* <p>If any thread is {@linkplain Thread#interrupt interrupted} while
...
...
@@ -412,16 +416,17 @@ public class CyclicBarrier {
* @param timeout the time to wait for the barrier
* @param unit the time unit of the timeout parameter
* @return the arrival index of the current thread, where index
*
<tt>{@link #getParties()} - 1</tt>
indicates the first
*
{@code getParties() - 1}
indicates the first
* to arrive and zero indicates the last to arrive
* @throws InterruptedException if the current thread was interrupted
* while waiting
* @throws TimeoutException if the specified timeout elapses
* @throws TimeoutException if the specified timeout elapses.
* In this case the barrier will be broken.
* @throws BrokenBarrierException if <em>another</em> thread was
* interrupted or timed out while the current thread was
* waiting, or the barrier was reset, or the barrier was broken
* when {@code await} was called, or the barrier action (if
* present) failed due an exception
* present) failed due
to
an exception
*/
public
int
await
(
long
timeout
,
TimeUnit
unit
)
throws
InterruptedException
,
...
...
src/share/classes/java/util/concurrent/Exchanger.java
浏览文件 @
aa86f2c8
此差异已折叠。
点击以展开。
src/share/classes/java/util/concurrent/Phaser.java
浏览文件 @
aa86f2c8
...
...
@@ -46,7 +46,7 @@ import java.util.concurrent.locks.LockSupport;
* {@link java.util.concurrent.CountDownLatch CountDownLatch}
* but supporting more flexible usage.
*
* <p>
<b>Registration.</b> Unlike the case for other barriers, the
* <p><b>Registration.</b> Unlike the case for other barriers, the
* number of parties <em>registered</em> to synchronize on a phaser
* may vary over time. Tasks may be registered at any time (using
* methods {@link #register}, {@link #bulkRegister}, or forms of
...
...
@@ -59,7 +59,7 @@ import java.util.concurrent.locks.LockSupport;
* (However, you can introduce such bookkeeping by subclassing this
* class.)
*
* <p>
<b>Synchronization.</b> Like a {@code CyclicBarrier}, a {@code
* <p><b>Synchronization.</b> Like a {@code CyclicBarrier}, a {@code
* Phaser} may be repeatedly awaited. Method {@link
* #arriveAndAwaitAdvance} has effect analogous to {@link
* java.util.concurrent.CyclicBarrier#await CyclicBarrier.await}. Each
...
...
@@ -103,7 +103,7 @@ import java.util.concurrent.locks.LockSupport;
*
* </ul>
*
* <p>
<b>Termination.</b> A phaser may enter a <em>termination</em>
* <p><b>Termination.</b> A phaser may enter a <em>termination</em>
* state, that may be checked using method {@link #isTerminated}. Upon
* termination, all synchronization methods immediately return without
* waiting for advance, as indicated by a negative return value.
...
...
@@ -118,7 +118,7 @@ import java.util.concurrent.locks.LockSupport;
* also available to abruptly release waiting threads and allow them
* to terminate.
*
* <p>
<b>Tiering.</b> Phasers may be <em>tiered</em> (i.e.,
* <p><b>Tiering.</b> Phasers may be <em>tiered</em> (i.e.,
* constructed in tree structures) to reduce contention. Phasers with
* large numbers of parties that would otherwise experience heavy
* synchronization contention costs may instead be set up so that
...
...
@@ -300,18 +300,20 @@ public class Phaser {
private
static
final
int
PHASE_SHIFT
=
32
;
private
static
final
int
UNARRIVED_MASK
=
0xffff
;
// to mask ints
private
static
final
long
PARTIES_MASK
=
0xffff0000
L
;
// to mask longs
private
static
final
long
COUNTS_MASK
=
0xffffffff
L
;
private
static
final
long
TERMINATION_BIT
=
1L
<<
63
;
// some special values
private
static
final
int
ONE_ARRIVAL
=
1
;
private
static
final
int
ONE_PARTY
=
1
<<
PARTIES_SHIFT
;
private
static
final
int
ONE_DEREGISTER
=
ONE_ARRIVAL
|
ONE_PARTY
;
private
static
final
int
EMPTY
=
1
;
// The following unpacking methods are usually manually inlined
private
static
int
unarrivedOf
(
long
s
)
{
int
counts
=
(
int
)
s
;
return
(
counts
==
EMPTY
)
?
0
:
counts
&
UNARRIVED_MASK
;
return
(
counts
==
EMPTY
)
?
0
:
(
counts
&
UNARRIVED_MASK
)
;
}
private
static
int
partiesOf
(
long
s
)
{
...
...
@@ -372,37 +374,44 @@ public class Phaser {
* Manually tuned to speed up and minimize race windows for the
* common case of just decrementing unarrived field.
*
* @param deregister false for arrive, true for arriveAndDeregister
* @param adjust value to subtract from state;
* ONE_ARRIVAL for arrive,
* ONE_DEREGISTER for arriveAndDeregister
*/
private
int
doArrive
(
boolean
deregister
)
{
int
adj
=
deregister
?
ONE_ARRIVAL
|
ONE_PARTY
:
ONE_ARRIVAL
;
private
int
doArrive
(
int
adjust
)
{
final
Phaser
root
=
this
.
root
;
for
(;;)
{
long
s
=
(
root
==
this
)
?
state
:
reconcileState
();
int
phase
=
(
int
)(
s
>>>
PHASE_SHIFT
);
int
counts
=
(
int
)
s
;
int
unarrived
=
(
counts
&
UNARRIVED_MASK
)
-
1
;
if
(
phase
<
0
)
return
phase
;
else
if
(
counts
==
EMPTY
||
unarrived
<
0
)
{
if
(
root
==
this
||
reconcileState
()
==
s
)
throw
new
IllegalStateException
(
badArrive
(
s
));
}
else
if
(
UNSAFE
.
compareAndSwapLong
(
this
,
stateOffset
,
s
,
s
-=
adj
))
{
if
(
unarrived
==
0
)
{
int
counts
=
(
int
)
s
;
int
unarrived
=
(
counts
==
EMPTY
)
?
0
:
(
counts
&
UNARRIVED_MASK
);
if
(
unarrived
<=
0
)
throw
new
IllegalStateException
(
badArrive
(
s
));
if
(
UNSAFE
.
compareAndSwapLong
(
this
,
stateOffset
,
s
,
s
-=
adjust
))
{
if
(
unarrived
==
1
)
{
long
n
=
s
&
PARTIES_MASK
;
// base of next state
int
nextUnarrived
=
(
int
)
n
>>>
PARTIES_SHIFT
;
if
(
root
!=
this
)
return
parent
.
doArrive
(
nextUnarrived
==
0
);
if
(
onAdvance
(
phase
,
nextUnarrived
))
n
|=
TERMINATION_BIT
;
else
if
(
nextUnarrived
==
0
)
n
|=
EMPTY
;
if
(
root
==
this
)
{
if
(
onAdvance
(
phase
,
nextUnarrived
))
n
|=
TERMINATION_BIT
;
else
if
(
nextUnarrived
==
0
)
n
|=
EMPTY
;
else
n
|=
nextUnarrived
;
int
nextPhase
=
(
phase
+
1
)
&
MAX_PHASE
;
n
|=
(
long
)
nextPhase
<<
PHASE_SHIFT
;
UNSAFE
.
compareAndSwapLong
(
this
,
stateOffset
,
s
,
n
);
releaseWaiters
(
phase
);
}
else
if
(
nextUnarrived
==
0
)
{
// propagate deregistration
phase
=
parent
.
doArrive
(
ONE_DEREGISTER
);
UNSAFE
.
compareAndSwapLong
(
this
,
stateOffset
,
s
,
s
|
EMPTY
);
}
else
n
|=
nextUnarrived
;
n
|=
(
long
)((
phase
+
1
)
&
MAX_PHASE
)
<<
PHASE_SHIFT
;
UNSAFE
.
compareAndSwapLong
(
this
,
stateOffset
,
s
,
n
);
releaseWaiters
(
phase
);
phase
=
parent
.
doArrive
(
ONE_ARRIVAL
);
}
return
phase
;
}
...
...
@@ -417,42 +426,49 @@ public class Phaser {
*/
private
int
doRegister
(
int
registrations
)
{
// adjustment to state
long
adj
=
((
long
)
registrations
<<
PARTIES_SHIFT
)
|
registrations
;
long
adj
ust
=
((
long
)
registrations
<<
PARTIES_SHIFT
)
|
registrations
;
final
Phaser
parent
=
this
.
parent
;
int
phase
;
for
(;;)
{
long
s
=
state
;
long
s
=
(
parent
==
null
)
?
state
:
reconcileState
()
;
int
counts
=
(
int
)
s
;
int
parties
=
counts
>>>
PARTIES_SHIFT
;
int
unarrived
=
counts
&
UNARRIVED_MASK
;
if
(
registrations
>
MAX_PARTIES
-
parties
)
throw
new
IllegalStateException
(
badRegister
(
s
));
else
if
((
phase
=
(
int
)(
s
>>>
PHASE_SHIFT
))
<
0
)
phase
=
(
int
)(
s
>>>
PHASE_SHIFT
);
if
(
phase
<
0
)
break
;
else
if
(
counts
!=
EMPTY
)
{
// not 1st registration
if
(
counts
!=
EMPTY
)
{
// not 1st registration
if
(
parent
==
null
||
reconcileState
()
==
s
)
{
if
(
unarrived
==
0
)
// wait out advance
root
.
internalAwaitAdvance
(
phase
,
null
);
else
if
(
UNSAFE
.
compareAndSwapLong
(
this
,
stateOffset
,
s
,
s
+
adj
))
s
,
s
+
adj
ust
))
break
;
}
}
else
if
(
parent
==
null
)
{
// 1st root registration
long
next
=
((
long
)
phase
<<
PHASE_SHIFT
)
|
adj
;
long
next
=
((
long
)
phase
<<
PHASE_SHIFT
)
|
adj
ust
;
if
(
UNSAFE
.
compareAndSwapLong
(
this
,
stateOffset
,
s
,
next
))
break
;
}
else
{
synchronized
(
this
)
{
// 1st sub registration
if
(
state
==
s
)
{
// recheck under lock
parent
.
doRegister
(
1
);
do
{
// force current phase
phase
=
parent
.
doRegister
(
1
);
if
(
phase
<
0
)
break
;
// finish registration whenever parent registration
// succeeded, even when racing with termination,
// since these are part of the same "transaction".
while
(!
UNSAFE
.
compareAndSwapLong
(
this
,
stateOffset
,
s
,
((
long
)
phase
<<
PHASE_SHIFT
)
|
adjust
))
{
s
=
state
;
phase
=
(
int
)(
root
.
state
>>>
PHASE_SHIFT
);
// assert phase < 0 || (int)state == EMPTY;
}
while
(!
UNSAFE
.
compareAndSwapLong
(
this
,
stateOffset
,
state
,
((
long
)
phase
<<
PHASE_SHIFT
)
|
adj
));
// assert (int)s == EMPTY;
}
break
;
}
}
...
...
@@ -467,10 +483,6 @@ public class Phaser {
* subphasers have not yet done so, in which case they must finish
* their own advance by setting unarrived to parties (or if
* parties is zero, resetting to unregistered EMPTY state).
* However, this method may also be called when "floating"
* subphasers with possibly some unarrived parties are merely
* catching up to current phase, in which case counts are
* unaffected.
*
* @return reconciled state
*/
...
...
@@ -478,16 +490,16 @@ public class Phaser {
final
Phaser
root
=
this
.
root
;
long
s
=
state
;
if
(
root
!=
this
)
{
int
phase
,
u
,
p
;
// CAS
root phase with current parties; possibly trip
unarrived
int
phase
,
p
;
// CAS
to root phase with current parties, tripping
unarrived
while
((
phase
=
(
int
)(
root
.
state
>>>
PHASE_SHIFT
))
!=
(
int
)(
s
>>>
PHASE_SHIFT
)
&&
!
UNSAFE
.
compareAndSwapLong
(
this
,
stateOffset
,
s
,
s
=
(((
long
)
phase
<<
PHASE_SHIFT
)
|
(
s
&
PARTIES_MASK
)
|
((
p
=
(
int
)
s
>>>
PARTIES_SHIFT
)
==
0
?
EMPTY
:
(
u
=
(
int
)
s
&
UNARRIVED_MASK
)
==
0
?
p
:
u
))))
(
(
phase
<
0
)
?
(
s
&
COUNTS_MASK
)
:
(((
p
=
(
int
)
s
>>>
PARTIES_SHIFT
)
==
0
)
?
EMPTY
:
((
s
&
PARTIES_MASK
)
|
p
))
))))
s
=
state
;
}
return
s
;
...
...
@@ -619,7 +631,7 @@ public class Phaser {
* of unarrived parties would become negative
*/
public
int
arrive
()
{
return
doArrive
(
false
);
return
doArrive
(
ONE_ARRIVAL
);
}
/**
...
...
@@ -639,7 +651,7 @@ public class Phaser {
* of registered or unarrived parties would become negative
*/
public
int
arriveAndDeregister
()
{
return
doArrive
(
true
);
return
doArrive
(
ONE_DEREGISTER
);
}
/**
...
...
@@ -666,17 +678,15 @@ public class Phaser {
for
(;;)
{
long
s
=
(
root
==
this
)
?
state
:
reconcileState
();
int
phase
=
(
int
)(
s
>>>
PHASE_SHIFT
);
int
counts
=
(
int
)
s
;
int
unarrived
=
(
counts
&
UNARRIVED_MASK
)
-
1
;
if
(
phase
<
0
)
return
phase
;
else
if
(
counts
==
EMPTY
||
unarrived
<
0
)
{
if
(
reconcileState
()
==
s
)
throw
new
IllegalStateException
(
badArrive
(
s
));
}
else
if
(
UNSAFE
.
compareAndSwapLong
(
this
,
stateOffset
,
s
,
s
-=
ONE_ARRIVAL
))
{
if
(
unarrived
!=
0
)
int
counts
=
(
int
)
s
;
int
unarrived
=
(
counts
==
EMPTY
)
?
0
:
(
counts
&
UNARRIVED_MASK
);
if
(
unarrived
<=
0
)
throw
new
IllegalStateException
(
badArrive
(
s
));
if
(
UNSAFE
.
compareAndSwapLong
(
this
,
stateOffset
,
s
,
s
-=
ONE_ARRIVAL
))
{
if
(
unarrived
>
1
)
return
root
.
internalAwaitAdvance
(
phase
,
null
);
if
(
root
!=
this
)
return
parent
.
arriveAndAwaitAdvance
();
...
...
@@ -809,8 +819,8 @@ public class Phaser {
if
(
UNSAFE
.
compareAndSwapLong
(
root
,
stateOffset
,
s
,
s
|
TERMINATION_BIT
))
{
// signal all threads
releaseWaiters
(
0
);
releaseWaiters
(
1
);
releaseWaiters
(
0
);
// Waiters on evenQ
releaseWaiters
(
1
);
// Waiters on oddQ
return
;
}
}
...
...
@@ -1016,7 +1026,7 @@ public class Phaser {
/**
* Possibly blocks and waits for phase to advance unless aborted.
* Call only
from root node
.
* Call only
on root phaser
.
*
* @param phase current phase
* @param node if non-null, the wait node to track interrupt and timeout;
...
...
@@ -1024,6 +1034,7 @@ public class Phaser {
* @return current phase
*/
private
int
internalAwaitAdvance
(
int
phase
,
QNode
node
)
{
// assert root == this;
releaseWaiters
(
phase
-
1
);
// ensure old queue clean
boolean
queued
=
false
;
// true when node is enqueued
int
lastUnarrived
=
0
;
// to increase spins upon change
...
...
@@ -1082,7 +1093,7 @@ public class Phaser {
final
boolean
timed
;
boolean
wasInterrupted
;
long
nanos
;
long
lastTim
e
;
final
long
deadlin
e
;
volatile
Thread
thread
;
// nulled to cancel wait
QNode
next
;
...
...
@@ -1093,7 +1104,7 @@ public class Phaser {
this
.
interruptible
=
interruptible
;
this
.
nanos
=
nanos
;
this
.
timed
=
timed
;
this
.
lastTime
=
timed
?
System
.
nanoTime
()
:
0L
;
this
.
deadline
=
timed
?
System
.
nanoTime
()
+
nanos
:
0L
;
thread
=
Thread
.
currentThread
();
}
...
...
@@ -1112,9 +1123,7 @@ public class Phaser {
}
if
(
timed
)
{
if
(
nanos
>
0L
)
{
long
now
=
System
.
nanoTime
();
nanos
-=
now
-
lastTime
;
lastTime
=
now
;
nanos
=
deadline
-
System
.
nanoTime
();
}
if
(
nanos
<=
0L
)
{
thread
=
null
;
...
...
@@ -1129,7 +1138,7 @@ public class Phaser {
return
true
;
else
if
(!
timed
)
LockSupport
.
park
(
this
);
else
if
(
nanos
>
0
)
else
if
(
nanos
>
0
L
)
LockSupport
.
parkNanos
(
this
,
nanos
);
return
isReleasable
();
}
...
...
src/share/classes/java/util/concurrent/TimeUnit.java
浏览文件 @
aa86f2c8
...
...
@@ -36,10 +36,10 @@
package
java.util.concurrent
;
/**
* A
<tt>TimeUnit</tt>
represents time durations at a given unit of
* A
{@code TimeUnit}
represents time durations at a given unit of
* granularity and provides utility methods to convert across units,
* and to perform timing and delay operations in these units. A
*
<tt>TimeUnit</tt>
does not maintain time information, but only
*
{@code TimeUnit}
does not maintain time information, but only
* helps organize and use time representations that may be maintained
* separately across various contexts. A nanosecond is defined as one
* thousandth of a microsecond, a microsecond as one thousandth of a
...
...
@@ -47,7 +47,7 @@ package java.util.concurrent;
* as sixty seconds, an hour as sixty minutes, and a day as twenty four
* hours.
*
* <p>A
<tt>TimeUnit</tt>
is mainly used to inform time-based methods
* <p>A
{@code TimeUnit}
is mainly used to inform time-based methods
* how a given timing parameter should be interpreted. For example,
* the following code will timeout in 50 milliseconds if the {@link
* java.util.concurrent.locks.Lock lock} is not available:
...
...
@@ -63,7 +63,7 @@ package java.util.concurrent;
*
* Note however, that there is no guarantee that a particular timeout
* implementation will be able to notice the passage of time at the
* same granularity as the given
<tt>TimeUnit</tt>
.
* same granularity as the given
{@code TimeUnit}
.
*
* @since 1.5
* @author Doug Lea
...
...
@@ -174,83 +174,82 @@ public enum TimeUnit {
// etc. are not declared abstract but otherwise act as abstract methods.
/**
* Convert the given time duration in the given unit to this
* unit. Conversions from finer to coarser granularities
* truncate, so lose precision. For example converting
* <tt>999</tt> milliseconds to seconds results in
* <tt>0</tt>. Conversions from coarser to finer granularities
* with arguments that would numerically overflow saturate to
* <tt>Long.MIN_VALUE</tt> if negative or <tt>Long.MAX_VALUE</tt>
* if positive.
* Converts the given time duration in the given unit to this unit.
* Conversions from finer to coarser granularities truncate, so
* lose precision. For example, converting {@code 999} milliseconds
* to seconds results in {@code 0}. Conversions from coarser to
* finer granularities with arguments that would numerically
* overflow saturate to {@code Long.MIN_VALUE} if negative or
* {@code Long.MAX_VALUE} if positive.
*
* <p>For example, to convert 10 minutes to milliseconds, use:
*
<tt>TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)</tt>
*
{@code TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)}
*
* @param sourceDuration the time duration in the given
<tt>sourceUnit</tt>
* @param sourceUnit the unit of the
<tt>sourceDuration</tt>
argument
* @param sourceDuration the time duration in the given
{@code sourceUnit}
* @param sourceUnit the unit of the
{@code sourceDuration}
argument
* @return the converted duration in this unit,
* or
<tt>Long.MIN_VALUE</tt>
if conversion would negatively
* overflow, or
<tt>Long.MAX_VALUE</tt>
if it would positively overflow.
* or
{@code Long.MIN_VALUE}
if conversion would negatively
* overflow, or
{@code Long.MAX_VALUE}
if it would positively overflow.
*/
public
long
convert
(
long
sourceDuration
,
TimeUnit
sourceUnit
)
{
throw
new
AbstractMethodError
();
}
/**
* Equivalent to <tt>NANOSECONDS.convert(duration, this)</tt>.
* Equivalent to
* {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}.
* @param duration the duration
* @return the converted duration,
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
* @see #convert
* or {@code Long.MIN_VALUE} if conversion would negatively
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
*/
public
long
toNanos
(
long
duration
)
{
throw
new
AbstractMethodError
();
}
/**
* Equivalent to <tt>MICROSECONDS.convert(duration, this)</tt>.
* Equivalent to
* {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}.
* @param duration the duration
* @return the converted duration,
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
* @see #convert
* or {@code Long.MIN_VALUE} if conversion would negatively
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
*/
public
long
toMicros
(
long
duration
)
{
throw
new
AbstractMethodError
();
}
/**
* Equivalent to <tt>MILLISECONDS.convert(duration, this)</tt>.
* Equivalent to
* {@link #convert(long, TimeUnit) MILLISECONDS.convert(duration, this)}.
* @param duration the duration
* @return the converted duration,
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
* @see #convert
* or {@code Long.MIN_VALUE} if conversion would negatively
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
*/
public
long
toMillis
(
long
duration
)
{
throw
new
AbstractMethodError
();
}
/**
* Equivalent to <tt>SECONDS.convert(duration, this)</tt>.
* Equivalent to
* {@link #convert(long, TimeUnit) SECONDS.convert(duration, this)}.
* @param duration the duration
* @return the converted duration,
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
* @see #convert
* or {@code Long.MIN_VALUE} if conversion would negatively
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
*/
public
long
toSeconds
(
long
duration
)
{
throw
new
AbstractMethodError
();
}
/**
* Equivalent to <tt>MINUTES.convert(duration, this)</tt>.
* Equivalent to
* {@link #convert(long, TimeUnit) MINUTES.convert(duration, this)}.
* @param duration the duration
* @return the converted duration,
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
* @see #convert
* or {@code Long.MIN_VALUE} if conversion would negatively
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
* @since 1.6
*/
public
long
toMinutes
(
long
duration
)
{
...
...
@@ -258,12 +257,12 @@ public enum TimeUnit {
}
/**
* Equivalent to <tt>HOURS.convert(duration, this)</tt>.
* Equivalent to
* {@link #convert(long, TimeUnit) HOURS.convert(duration, this)}.
* @param duration the duration
* @return the converted duration,
* or <tt>Long.MIN_VALUE</tt> if conversion would negatively
* overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
* @see #convert
* or {@code Long.MIN_VALUE} if conversion would negatively
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
* @since 1.6
*/
public
long
toHours
(
long
duration
)
{
...
...
@@ -271,10 +270,10 @@ public enum TimeUnit {
}
/**
* Equivalent to <tt>DAYS.convert(duration, this)</tt>.
* Equivalent to
* {@link #convert(long, TimeUnit) DAYS.convert(duration, this)}.
* @param duration the duration
* @return the converted duration
* @see #convert
* @since 1.6
*/
public
long
toDays
(
long
duration
)
{
...
...
@@ -294,9 +293,9 @@ public enum TimeUnit {
* Performs a timed {@link Object#wait(long, int) Object.wait}
* using this time unit.
* This is a convenience method that converts timeout arguments
* into the form required by the
<tt>Object.wait</tt>
method.
* into the form required by the
{@code Object.wait}
method.
*
* <p>For example, you could implement a blocking
<tt>poll</tt>
* <p>For example, you could implement a blocking
{@code poll}
* method (see {@link BlockingQueue#poll BlockingQueue.poll})
* using:
*
...
...
@@ -327,7 +326,7 @@ public enum TimeUnit {
* Performs a timed {@link Thread#join(long, int) Thread.join}
* using this time unit.
* This is a convenience method that converts time arguments into the
* form required by the
<tt>Thread.join</tt>
method.
* form required by the
{@code Thread.join}
method.
*
* @param thread the thread to wait for
* @param timeout the maximum time to wait. If less than
...
...
@@ -347,7 +346,7 @@ public enum TimeUnit {
* Performs a {@link Thread#sleep(long, int) Thread.sleep} using
* this time unit.
* This is a convenience method that converts time arguments into the
* form required by the
<tt>Thread.sleep</tt>
method.
* form required by the
{@code Thread.sleep}
method.
*
* @param timeout the minimum time to sleep. If less than
* or equal to zero, do not sleep at all.
...
...
src/share/classes/java/util/concurrent/TimeoutException.java
浏览文件 @
aa86f2c8
...
...
@@ -40,7 +40,7 @@ package java.util.concurrent;
* operations for which a timeout is specified need a means to
* indicate that the timeout has occurred. For many such operations it
* is possible to return a value that indicates timeout; when that is
* not possible or desirable then
<tt>TimeoutException</tt>
should be
* not possible or desirable then
{@code TimeoutException}
should be
* declared and thrown.
*
* @since 1.5
...
...
@@ -50,13 +50,13 @@ public class TimeoutException extends Exception {
private
static
final
long
serialVersionUID
=
1900926677490660714L
;
/**
* Constructs a
<tt>TimeoutException</tt>
with no specified detail
* Constructs a
{@code TimeoutException}
with no specified detail
* message.
*/
public
TimeoutException
()
{}
/**
* Constructs a
<tt>TimeoutException</tt>
with the specified detail
* Constructs a
{@code TimeoutException}
with the specified detail
* message.
*
* @param message the detail message
...
...
src/share/classes/java/util/concurrent/package-info.java
浏览文件 @
aa86f2c8
...
...
@@ -48,7 +48,7 @@
*
* {@link java.util.concurrent.Executor} is a simple standardized
* interface for defining custom thread-like subsystems, including
* thread pools, asynchronous IO, and lightweight task frameworks.
* thread pools, asynchronous I
/
O, and lightweight task frameworks.
* Depending on which concrete Executor class is being used, tasks may
* execute in a newly created thread, an existing task-execution thread,
* or the thread calling {@link java.util.concurrent.Executor#execute
...
...
@@ -102,8 +102,10 @@
* <h2>Queues</h2>
*
* The {@link java.util.concurrent.ConcurrentLinkedQueue} class
* supplies an efficient scalable thread-safe non-blocking FIFO
* queue.
* supplies an efficient scalable thread-safe non-blocking FIFO queue.
* The {@link java.util.concurrent.ConcurrentLinkedDeque} class is
* similar, but additionally supports the {@link java.util.Deque}
* interface.
*
* <p>Five implementations in {@code java.util.concurrent} support
* the extended {@link java.util.concurrent.BlockingQueue}
...
...
@@ -117,7 +119,7 @@
* for producer-consumer, messaging, parallel tasking, and
* related concurrent designs.
*
* <p>
Extended interface {@link java.util.concurrent.TransferQueue},
* <p>Extended interface {@link java.util.concurrent.TransferQueue},
* and implementation {@link java.util.concurrent.LinkedTransferQueue}
* introduce a synchronous {@code transfer} method (along with related
* features) in which a producer may optionally block awaiting its
...
...
@@ -216,9 +218,9 @@
* it may (or may not) reflect any updates since the iterator was
* created.
*
* <h2
><a name="MemoryVisibility">Memory Consistency Properties</a>
</h2>
* <h2
id="MemoryVisibility">Memory Consistency Properties
</h2>
*
* <a href="http://docs.oracle.com/javase/specs/jls/se7/html/
index.html
">
* <a href="http://docs.oracle.com/javase/specs/jls/se7/html/
jls-17.html#jls-17.4.5
">
* Chapter 17 of the Java Language Specification</a> defines the
* <i>happens-before</i> relation on memory operations such as reads and
* writes of shared variables. The results of a write by one thread are
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录