Java Doc for Condition.java in  » Library » Apache-beehive-1.0.2-src » org » apache » beehive » netui » util » internal » concurrent » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache beehive 1.0.2 src » org.apache.beehive.netui.util.internal.concurrent 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


org.apache.beehive.netui.util.internal.concurrent.Condition

All known Subclasses:   org.apache.beehive.netui.util.internal.concurrent.CondVar,
Condition
interface Condition (Code)
Condition factors out the Object monitor methods ( Object.wait wait , Object.notify notify and Object.notifyAll notifyAll ) into distinct objects to give the effect of having multiple wait-sets per object, by combining them with the use of arbitrary Lock implementations. Where a Lock replaces the use of synchronized methods and statements, a Condition replaces the use of the Object monitor methods.

Conditions (also known as condition queues or condition variables) provide a means for one thread to suspend execution (to "wait") until notified by another thread that some state condition may now be true. Because access to this shared state information occurs in different threads, it must be protected, so a lock of some form is associated with the condition. The key property that waiting for a condition provides is that it atomically releases the associated lock and suspends the current thread, just like Object.wait.

A Condition instance is intrinsically bound to a lock. To obtain a Condition instance for a particular Lock instance use its Lock.newCondition newCondition() method.

As an example, suppose we have a bounded buffer which supports put and take methods. If a take is attempted on an empty buffer, then the thread will block until an item becomes available; if a put is attempted on a full buffer, then the thread will block until a space becomes available. We would like to keep waiting put threads and take threads in separate wait-sets so that we can use the optimization of only notifying a single thread at a time when items or spaces become available in the buffer. This can be achieved using two Condition instances.

 class BoundedBuffer {
 final Lock lock = new ReentrantLock();
 final Condition notFull  = lock.newCondition(); 
 final Condition notEmpty = lock.newCondition(); 
 final Object[] items = new Object[100];
 int putptr, takeptr, count;
 public void put(Object x) throws InterruptedException {
 lock.lock();
 try {
 while (count == items.length)
 notFull.await();
 items[putptr] = x;
 if (++putptr == items.length) putptr = 0;
 ++count;
 notEmpty.signal();
 } finally {
 lock.unlock();
 }
 }
 public Object take() throws InterruptedException {
 lock.lock();
 try {
 while (count == 0)
 notEmpty.await();
 Object x = items[takeptr];
 if (++takeptr == items.length) takeptr = 0;
 --count;
 notFull.signal();
 return x;
 } finally {
 lock.unlock();
 }
 }
 }
 
(The org.apache.beehive.netui.util.concurrent.ArrayBlockingQueue class provides this functionality, so there is no reason to implement this sample usage class.)

A Condition implementation can provide behavior and semantics that is different from that of the Object monitor methods, such as guaranteed ordering for notifications, or not requiring a lock to be held when performing notifications. If an implementation provides such specialized semantics then the implementation must document those semantics.

Note that Condition instances are just normal objects and can themselves be used as the target in a synchronized statement, and can have their own monitor Object.wait wait and Object.notify notification methods invoked. Acquiring the monitor lock of a Condition instance, or using its monitor methods, has no specified relationship with acquiring the Lock associated with that Condition or the use of its Condition.await waiting and Condition.signal signalling methods. It is recommended that to avoid confusion you never use Condition instances in this way, except perhaps within their own implementation.

Except where noted, passing a null value for any parameter will result in a NullPointerException being thrown.

Implementation Considerations

When waiting upon a Condition, a "spurious wakeup" is permitted to occur, in general, as a concession to the underlying platform semantics. This has little practical impact on most application programs as a Condition should always be waited upon in a loop, testing the state predicate that is being waited for. An implementation is free to remove the possibility of spurious wakeups but it is recommended that applications programmers always assume that they can occur and so always wait in a loop.

The three forms of condition waiting (interruptible, non-interruptible, and timed) may differ in their ease of implementation on some platforms and in their performance characteristics. In particular, it may be difficult to provide these features and maintain specific semantics such as ordering guarantees. Further, the ability to interrupt the actual suspension of the thread may not always be feasible to implement on all platforms.

Consequently, an implementation is not required to define exactly the same guarantees or semantics for all three forms of waiting, nor is it required to support interruption of the actual suspension of the thread.

An implementation is required to clearly document the semantics and guarantees provided by each of the waiting methods, and when an implementation does support interruption of thread suspension then it must obey the interruption semantics as defined in this interface.

As interruption generally implies cancellation, and checks for interruption are often infrequent, an implementation can favor responding to an interrupt over normal method return. This is true even if it can be shown that the interrupt occurred after another action may have unblocked the thread. An implementation should document this behavior.
since:
   1.5
author:
   Doug Lea





Method Summary
 voidawait()
     Causes the current thread to wait until it is signalled or Thread.interrupt interrupted .

The lock associated with this Condition is atomically released and the current thread becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:

  • Some other thread invokes the Condition.signal method for this Condition and the current thread happens to be chosen as the thread to be awakened; or
  • Some other thread invokes the Condition.signalAll method for this Condition; or
  • Some other thread Thread.interrupt interrupts the current thread, and interruption of thread suspension is supported; or
  • A "spurious wakeup" occurs

In all cases, before this method can return the current thread must re-acquire the lock associated with this condition.

 booleanawait(long time, TimeUnit unit)
     Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses.
 voidawaitUninterruptibly()
     Causes the current thread to wait until it is signalled.

The lock associated with this condition is atomically released and the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:

  • Some other thread invokes the Condition.signal method for this Condition and the current thread happens to be chosen as the thread to be awakened; or
  • Some other thread invokes the Condition.signalAll method for this Condition; or
  • A "spurious wakeup" occurs

In all cases, before this method can return the current thread must re-acquire the lock associated with this condition.

 booleanawaitUntil(Date deadline)
     Causes the current thread to wait until it is signalled or interrupted, or the specified deadline elapses.

The lock associated with this condition is atomically released and the current thread becomes disabled for thread scheduling purposes and lies dormant until one of five things happens:

  • Some other thread invokes the Condition.signal method for this Condition and the current thread happens to be chosen as the thread to be awakened; or
  • Some other thread invokes the Condition.signalAll method for this Condition; or
  • Some other thread Thread.interrupt interrupts the current thread, and interruption of thread suspension is supported; or
  • The specified deadline elapses; or
  • A "spurious wakeup" occurs.

In all cases, before this method can return the current thread must re-acquire the lock associated with this condition.

 voidsignal()
     Wakes up one waiting thread.

If any threads are waiting on this condition then one is selected for waking up.

 voidsignalAll()
     Wakes up all waiting threads.

If any threads are waiting on this condition then they are all woken up.




Method Detail
await
void await() throws InterruptedException(Code)
Causes the current thread to wait until it is signalled or Thread.interrupt interrupted .

The lock associated with this Condition is atomically released and the current thread becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:

  • Some other thread invokes the Condition.signal method for this Condition and the current thread happens to be chosen as the thread to be awakened; or
  • Some other thread invokes the Condition.signalAll method for this Condition; or
  • Some other thread Thread.interrupt interrupts the current thread, and interruption of thread suspension is supported; or
  • A "spurious wakeup" occurs

In all cases, before this method can return the current thread must re-acquire the lock associated with this condition. When the thread returns it is guaranteed to hold this lock.

If the current thread:

  • has its interrupted status set on entry to this method; or
  • is Thread.interrupt interrupted while waiting and interruption of thread suspension is supported,
then InterruptedException is thrown and the current thread's interrupted status is cleared. It is not specified, in the first case, whether or not the test for interruption occurs before the lock is released.

Implementation Considerations

The current thread is assumed to hold the lock associated with this Condition when this method is called. It is up to the implementation to determine if this is the case and if not, how to respond. Typically, an exception will be thrown (such as IllegalMonitorStateException ) and the implementation must document that fact.

An implementation can favor responding to an interrupt over normal method return in response to a signal. In that case the implementation must ensure that the signal is redirected to another waiting thread, if there is one.
throws:
  InterruptedException - if the current thread is interrupted (andinterruption of thread suspension is supported).




await
boolean await(long time, TimeUnit unit) throws InterruptedException(Code)
Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses. This method is behaviorally equivalent to:
 awaitNanos(unit.toNanos(time)) > 0
 

Parameters:
  time - the maximum time to wait
Parameters:
  unit - the time unit of the time argument. false if the waiting time detectably elapsedbefore return from the method, else true.
throws:
  InterruptedException - if the current thread is interrupted (andinterruption of thread suspension is supported).



awaitUninterruptibly
void awaitUninterruptibly()(Code)
Causes the current thread to wait until it is signalled.

The lock associated with this condition is atomically released and the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:

  • Some other thread invokes the Condition.signal method for this Condition and the current thread happens to be chosen as the thread to be awakened; or
  • Some other thread invokes the Condition.signalAll method for this Condition; or
  • A "spurious wakeup" occurs

In all cases, before this method can return the current thread must re-acquire the lock associated with this condition. When the thread returns it is guaranteed to hold this lock.

If the current thread's interrupted status is set when it enters this method, or it is Thread.interrupt interrupted while waiting, it will continue to wait until signalled. When it finally returns from this method its interrupted status will still be set.

Implementation Considerations

The current thread is assumed to hold the lock associated with this Condition when this method is called. It is up to the implementation to determine if this is the case and if not, how to respond. Typically, an exception will be thrown (such as IllegalMonitorStateException ) and the implementation must document that fact.




awaitUntil
boolean awaitUntil(Date deadline) throws InterruptedException(Code)
Causes the current thread to wait until it is signalled or interrupted, or the specified deadline elapses.

The lock associated with this condition is atomically released and the current thread becomes disabled for thread scheduling purposes and lies dormant until one of five things happens:

  • Some other thread invokes the Condition.signal method for this Condition and the current thread happens to be chosen as the thread to be awakened; or
  • Some other thread invokes the Condition.signalAll method for this Condition; or
  • Some other thread Thread.interrupt interrupts the current thread, and interruption of thread suspension is supported; or
  • The specified deadline elapses; or
  • A "spurious wakeup" occurs.

In all cases, before this method can return the current thread must re-acquire the lock associated with this condition. When the thread returns it is guaranteed to hold this lock.

If the current thread:

  • has its interrupted status set on entry to this method; or
  • is Thread.interrupt interrupted while waiting and interruption of thread suspension is supported,
then InterruptedException is thrown and the current thread's interrupted status is cleared. It is not specified, in the first case, whether or not the test for interruption occurs before the lock is released.

The return value indicates whether the deadline has elapsed, which can be used as follows:

 synchronized boolean aMethod(Date deadline) {
 boolean stillWaiting = true;
 while (!conditionBeingWaitedFor) {
 if (stillwaiting)
 stillWaiting = theCondition.awaitUntil(deadline);
 else
 return false;
 }
 // ...
 }
 

Implementation Considerations

The current thread is assumed to hold the lock associated with this Condition when this method is called. It is up to the implementation to determine if this is the case and if not, how to respond. Typically, an exception will be thrown (such as IllegalMonitorStateException ) and the implementation must document that fact.

An implementation can favor responding to an interrupt over normal method return in response to a signal, or over indicating the passing of the specified deadline. In either case the implementation must ensure that the signal is redirected to another waiting thread, if there is one.
Parameters:
  deadline - the absolute time to wait until false if the deadline haselapsed upon return, else true.
throws:
  InterruptedException - if the current thread is interrupted (andinterruption of thread suspension is supported).




signal
void signal()(Code)
Wakes up one waiting thread.

If any threads are waiting on this condition then one is selected for waking up. That thread must then re-acquire the lock before returning from await.




signalAll
void signalAll()(Code)
Wakes up all waiting threads.

If any threads are waiting on this condition then they are all woken up. Each thread must re-acquire the lock before it can return from await.




www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.