Java Doc for Mutex.java in  » IDE-Netbeans » openide » org » openide » util » 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 » IDE Netbeans » openide » org.openide.util 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.openide.util.Mutex

Mutex
final public class Mutex extends Object (Code)
Read-many/write-one lock. Allows control over resources that can be read by several readers at once but only written by one writer.

It is guaranteed that if you are a writer you can also enter the mutex as a reader. Conversely, if you are the only reader you can enter the mutex as a writer, but you'll be warned because it is very deadlock prone (two readers trying to get write access concurently).

If the mutex is used only by one thread, the thread can repeatedly enter it as a writer or reader. So one thread can never deadlock itself, whichever order operations are performed in.

There is no strategy to prevent starvation. Even if there is a writer waiting to enter, another reader might enter the section instead.

Examples of use:

 Mutex m = new Mutex ();
 // Grant write access, compute an integer and return it:
 return (Integer)m.writeAccess (new Mutex.Action () {
 public Object run () {
 return new Integer (1);
 }
 });
 // Obtain read access, do some computation, possibly throw an IOException:
 try {
 m.readAccess (new Mutex.ExceptionAction () {
 public Object run () throws IOException {
 if (...) throw new IOException ();
 return null;
 }
 });
 } catch (MutexException ex) {
 throw (IOException)ex.getException ();
 }
 // check whether you are already in read access
 if (m.isReadAccess ()) {
 // do your work
 }
 

author:
   Ales Novak

Inner Class :public interface Action extends ExceptionAction<T>
Inner Class :public interface ExceptionAction
Inner Class :final public static class Privileged

Field Summary
final public static  MutexEVENT
     Mutex that allows code to be synchronized with the AWT event dispatch thread.
static  booleanbeStrict
     this is used from tests to prevent upgrade from readAccess to writeAccess by strictly throwing exception.

Constructor Summary
public  Mutex(Object lock)
     Enhanced constructor that permits specifying an object to use as a lock. The lock is used on entry and exit to Mutex.readAccess and during the whole execution of Mutex.writeAccess .
public  Mutex()
     Default constructor.
public  Mutex(Privileged privileged)
    
public  Mutex(Privileged privileged, Executor executor)
     Constructor for those who wish to do some custom additional tasks whenever an action or runnable is executed in the Mutex .

Method Summary
static  booleanisDispatchThread()
    
public  booleanisReadAccess()
     Tests whether this thread has already entered the mutex in read access.
public  booleanisWriteAccess()
     Tests whether this thread has already entered the mutex in write access. If it returns true, calling writeAccess will be executed immediatelly without any other blocking.
public  voidpostReadRequest(Runnable run)
     Posts a read request.
public  voidpostWriteRequest(Runnable run)
     Posts a write request.
public  TreadAccess(Action<T> action)
     Run an action only with read access. See class description re.
public  TreadAccess(ExceptionAction<T> action)
     Run an action with read access and possibly throw a checked exception. The exception if thrown is then encapsulated in a MutexException and thrown from this method.
public  voidreadAccess(Runnable action)
     Run an action with read access, returning no result.
public  StringtoString()
    
public  TwriteAccess(Action<T> action)
     Run an action with write access.
public  TwriteAccess(ExceptionAction<T> action)
     Run an action with write access and possibly throw an exception.
public  voidwriteAccess(Runnable action)
     Run an action with write access and return no result.

Field Detail
EVENT
final public static Mutex EVENT(Code)
Mutex that allows code to be synchronized with the AWT event dispatch thread.

When the Mutex methods are invoked on this mutex, the methods' semantics change as follows:




beStrict
static boolean beStrict(Code)
this is used from tests to prevent upgrade from readAccess to writeAccess by strictly throwing exception. Otherwise we just notify that using ErrorManager.




Constructor Detail
Mutex
public Mutex(Object lock)(Code)
Enhanced constructor that permits specifying an object to use as a lock. The lock is used on entry and exit to Mutex.readAccess and during the whole execution of Mutex.writeAccess . The ability to specify locks allows several Mutexes to synchronize on one object or to synchronize a mutex with another critical section.
Parameters:
  lock - lock to use



Mutex
public Mutex()(Code)
Default constructor.



Mutex
public Mutex(Privileged privileged)(Code)

Parameters:
  privileged - can enter privileged states of this MutexThis helps avoid creating of custom Runnables.



Mutex
public Mutex(Privileged privileged, Executor executor)(Code)
Constructor for those who wish to do some custom additional tasks whenever an action or runnable is executed in the Mutex . This may be useful for wrapping all the actions with custom ThreadLocal value, etc. Just implement the Executor 's execute(Runnable) method and do pre and post initialization tasks before running the runnable.

The Executor.execute method shall return only when the passed in Runnable is finished, otherwise methods like Mutex.readAccess(Action) and co. might not return proper result.
Parameters:
  privileged - can enter privileged states of this Mutex
Parameters:
  executor - allows to wrap the work of the mutex with a custom code
since:
   7.12





Method Detail
isDispatchThread
static boolean isDispatchThread()(Code)
true iff current thread is EventDispatchThread



isReadAccess
public boolean isReadAccess()(Code)
Tests whether this thread has already entered the mutex in read access. If it returns true, calling readAccess will be executed immediatelly without any blocking. Calling postWriteAccess will delay the execution of its Runnable until a readAccess section is over and calling writeAccess is strongly prohibited and will result in a warning as a deadlock prone behaviour.

Warning: since a thread with write access automatically has effective read access as well (whether or not explicitly requested), if you want to check whether a thread can read some data, you should check for either kind of access, e.g.:

assert myMutex.isReadAccess() || myMutex.isWriteAccess();
true if the thread is in read access section
since:
   4.48



isWriteAccess
public boolean isWriteAccess()(Code)
Tests whether this thread has already entered the mutex in write access. If it returns true, calling writeAccess will be executed immediatelly without any other blocking. postReadAccess will be delayed until a write access runnable is over. true if the thread is in write access section
since:
   4.48



postReadRequest
public void postReadRequest(Runnable run)(Code)
Posts a read request. This request runs immediately iff this Mutex is in the shared mode or this Mutex is not contended at all. This request is delayed if this Mutex is in the exclusive mode and is held by this thread, until the exclusive is left. Finally, this request blocks, if this Mutex is in the exclusive mode and is held by another thread.

Warning: this method blocks.


Parameters:
  run - runnable to run



postWriteRequest
public void postWriteRequest(Runnable run)(Code)
Posts a write request. This request runs immediately iff this Mutex is in the "pure" exclusive mode, i.e. this Mutex is not reentered in shared mode after the exclusive mode was acquired. Otherwise it is delayed until all read requests are executed. This request runs immediately if this Mutex is not contended at all. This request blocks if this Mutex is in the shared mode.

Warning: this method blocks.


Parameters:
  run - runnable to run



readAccess
public T readAccess(Action<T> action)(Code)
Run an action only with read access. See class description re. entering for write access within the dynamic scope.
Parameters:
  action - the action to perform the object returned from Mutex.Action.run



readAccess
public T readAccess(ExceptionAction<T> action) throws MutexException(Code)
Run an action with read access and possibly throw a checked exception. The exception if thrown is then encapsulated in a MutexException and thrown from this method. One is encouraged to catch MutexException, obtain the inner exception, and rethrow it. Here is an example:

 try {
 mutex.readAccess (new ExceptionAction () {
 public void run () throws IOException {
 throw new IOException ();
 }
 });
 } catch (MutexException ex) {
 throw (IOException) ex.getException ();
 }
 
Note that runtime exceptions are always passed through, and neither require this invocation style, nor are encapsulated.
Parameters:
  action - the action to execute the object returned from Mutex.ExceptionAction.run
exception:
  MutexException - encapsulates a user exception
exception:
  RuntimeException - if any runtime exception is thrown from the run method
See Also:   Mutex.readAccess(Mutex.Action)



readAccess
public void readAccess(Runnable action)(Code)
Run an action with read access, returning no result. It may be run asynchronously.
Parameters:
  action - the action to perform
See Also:   Mutex.readAccess(Mutex.Action)



toString
public String toString()(Code)
toString



writeAccess
public T writeAccess(Action<T> action)(Code)
Run an action with write access. The same thread may meanwhile reenter the mutex; see the class description for details.
Parameters:
  action - the action to perform the result of Mutex.Action.run



writeAccess
public T writeAccess(ExceptionAction<T> action) throws MutexException(Code)
Run an action with write access and possibly throw an exception. Here is an example:

 try {
 mutex.writeAccess (new ExceptionAction () {
 public void run () throws IOException {
 throw new IOException ();
 }
 });
 } catch (MutexException ex) {
 throw (IOException) ex.getException ();
 }
 

Parameters:
  action - the action to execute the result of Mutex.ExceptionAction.run
exception:
  MutexException - an encapsulated checked exception, if any
exception:
  RuntimeException - if a runtime exception is thrown in the action
See Also:   Mutex.writeAccess(Mutex.Action)
See Also:   Mutex.readAccess(Mutex.ExceptionAction)



writeAccess
public void writeAccess(Runnable action)(Code)
Run an action with write access and return no result. It may be run asynchronously.
Parameters:
  action - the action to perform
See Also:   Mutex.writeAccess(Mutex.Action)
See Also:   Mutex.readAccess(Runnable)



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

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