Java Doc for SynchronizedVariable.java in  » Ajax » Laszlo-4.0.10 » EDU » oswego » cs » dl » util » 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 » Ajax » Laszlo 4.0.10 » EDU.oswego.cs.dl.util.concurrent 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   EDU.oswego.cs.dl.util.concurrent.SynchronizedVariable

All known Subclasses:   EDU.oswego.cs.dl.util.concurrent.SynchronizedFloat,  EDU.oswego.cs.dl.util.concurrent.SynchronizedShort,  EDU.oswego.cs.dl.util.concurrent.SynchronizedInt,  EDU.oswego.cs.dl.util.concurrent.SynchronizedLong,  EDU.oswego.cs.dl.util.concurrent.SynchronizedDouble,  EDU.oswego.cs.dl.util.concurrent.SynchronizedChar,  EDU.oswego.cs.dl.util.concurrent.SynchronizedByte,  EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean,  EDU.oswego.cs.dl.util.concurrent.SynchronizedRef,
SynchronizedVariable
public class SynchronizedVariable implements Executor(Code)
Base class for simple, small classes maintaining single values that are always accessed and updated under synchronization. Since defining them for only some types seemed too arbitrary, they exist for all basic types, although it is hard to imagine uses for some.

These classes mainly exist so that you do not have to go to the trouble of writing your own miscellaneous classes and methods in situations including:

  • When you need or want to offload an instance variable to use its own synchronization lock. When these objects are used to replace instance variables, they should almost always be declared as final. This helps avoid the need to synchronize just to obtain the reference to the synchronized variable itself.
  • When you need methods such as set, commit, or swap. Note however that the synchronization for these variables is independent of any other synchronization perfromed using other locks. So, they are not normally useful when accesses and updates among variables must be coordinated. For example, it would normally be a bad idea to make a Point class out of two SynchronizedInts, even those sharing a lock.
  • When defining static variables. It almost always works out better to rely on synchronization internal to these objects, rather than class locks.

While they cannot, by nature, share much code, all of these classes work in the same way.

Construction
Synchronized variables are always constructed holding an initial value of the associated type. Constructors also establish the lock to use for all methods:

  • By default, each variable uses itself as the synchronization lock. This is the most common choice in the most common usage contexts in which SynchronizedVariables are used to split off synchronization locks for independent attributes of a class.
  • You can specify any other Object to use as the synchronization lock. This allows you to use various forms of `slave synchronization'. For example, a variable that is always associated with a particular object can use that object's lock.

Update methods
Each class supports several kinds of update methods:

  • A set method that sets to a new value and returns previous value. For example, for a SynchronizedBoolean b, boolean old = b.set(true) performs a test-and-set.

  • A commit method that sets to new value only if currently holding a given value. For example, here is a class that uses an optimistic update loop to recompute a count variable represented as a SynchronizedInt.
     class X {
     private final SynchronizedInt count = new SynchronizedInt(0);
     static final int MAX_RETRIES = 1000;
     public boolean recomputeCount() throws InterruptedException {
     for (int i = 0; i < MAX_RETRIES; ++i) {
     int current = count.get();
     int next = compute(current);
     if (count.commit(current, next))
     return true;
     else if (Thread.interrupted()) 
     throw new InterruptedException();
     }
     return false;
     }
     int compute(int l) { ... some kind of computation ...  }
     }
     

  • A swap method that atomically swaps with another object of the same class using a deadlock-avoidance strategy.

  • Update-in-place methods appropriate to the type. All numerical types support:
    • add(x) (equivalent to return value += x)
    • subtract(x) (equivalent to return value -= x)
    • multiply(x) (equivalent to return value *= x)
    • divide(x) (equivalent to return value /= x)
    Integral types also support:
    • increment() (equivalent to return ++value)
    • decrement() (equivalent to return --value)
    Boolean types support:
    • or(x) (equivalent to return value |= x)
    • and(x) (equivalent to return value &= x)
    • xor(x) (equivalent to return value ^= x)
    • complement() (equivalent to return x = !x)
    These cover most, but not all of the possible operators in Java. You can add more compute-and-set methods in subclasses. This is often a good way to avoid the need for ad-hoc synchronized blocks surrounding expressions.

Guarded methods
All Waitable subclasses provide notifications on every value update, and support guarded methods of the form whenpredicate, that wait until the predicate hold, then optionally run any Runnable action within the lock, and then return. All types support:

  • whenEqual(value, action)
  • whenNotEqual(value, action)
(If the action argument is null, these return immediately after the predicate holds.) Numerical types also support
  • whenLess(value, action)
  • whenLessEqual(value, action)
  • whenGreater(value, action)
  • whenGreaterEqual(value, action)
The Waitable classes are not always spectacularly efficient since they provide notifications on all value changes. They are designed for use in contexts where either performance is not an overriding issue, or where nearly every update releases guarded waits anyway.

Other methods
This class implements Executor, and provides an execute method that runs the runnable within the lock.

All classes except SynchronizedRef and WaitableRef implement Cloneable and Comparable. Implementations of the corresponding methods either use default mechanics, or use methods that closely correspond to their java.lang analogs. SynchronizedRef does not implement any of these standard interfaces because there are many cases where it would not make sense. However, you can easily make simple subclasses that add the appropriate declarations.

[ Introduction to this package. ]



Field Summary
final protected  Objectlock_
    

Constructor Summary
public  SynchronizedVariable(Object lock)
    
public  SynchronizedVariable()
    

Method Summary
public  voidexecute(Runnable command)
    
public  ObjectgetLock()
    

Field Detail
lock_
final protected Object lock_(Code)




Constructor Detail
SynchronizedVariable
public SynchronizedVariable(Object lock)(Code)
Create a SynchronizedVariable using the supplied lock *



SynchronizedVariable
public SynchronizedVariable()(Code)
Create a SynchronizedVariable using itself as the lock *




Method Detail
execute
public void execute(Runnable command) throws InterruptedException(Code)
If current thread is not interrupted, execute the given command within this object's lock



getLock
public Object getLock()(Code)
Return the lock used for all synchronization for this object



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.