Java Doc for Sync.java in  » Testing » DbUnit » org » dbunit » 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 » Testing » DbUnit » org.dbunit.util.concurrent 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


org.dbunit.util.concurrent.Sync

All known Subclasses:   org.dbunit.util.concurrent.Semaphore,
Sync
public interface Sync (Code)
Main interface for locks, gates, and conditions.

Sync objects isolate waiting and notification for particular logical states, resource availability, events, and the like that are shared across multiple threads. Use of Syncs sometimes (but by no means always) adds flexibility and efficiency compared to the use of plain java monitor methods and locking, and are sometimes (but by no means always) simpler to program with.

Most Syncs are intended to be used primarily (although not exclusively) in before/after constructions such as:

 class X {
 Sync gate;
 // ...
 public void m() { 
 try {
 gate.acquire();  // block until condition holds
 try {
 // ... method body
 }
 finally {
 gate.release()
 }
 }
 catch (InterruptedException ex) {
 // ... evasive action
 }
 }
 public void m2(Sync cond) { // use supplied condition
 try {
 if (cond.attempt(10)) {         // try the condition for 10 ms
 try {
 // ... method body
 }
 finally {
 cond.release()
 }
 }
 }
 catch (InterruptedException ex) {
 // ... evasive action
 }
 }
 }
 
Syncs may be used in somewhat tedious but more flexible replacements for built-in Java synchronized blocks. For example:
 class HandSynched {          
 private double state_ = 0.0; 
 private final Sync lock;  // use lock type supplied in constructor
 public HandSynched(Sync l) { lock = l; } 
 public void changeState(double d) {
 try {
 lock.acquire(); 
 try     { state_ = updateFunction(d); } 
 finally { lock.release(); }
 } 
 catch(InterruptedException ex) { }
 }
 public double getState() {
 double d = 0.0;
 try {
 lock.acquire(); 
 try     { d = accessFunction(state_); }
 finally { lock.release(); }
 } 
 catch(InterruptedException ex){}
 return d;
 }
 private double updateFunction(double d) { ... }
 private double accessFunction(double d) { ... }
 }
 
If you have a lot of such methods, and they take a common form, you can standardize this using wrappers. Some of these wrappers are standardized in LockedExecutor, but you can make others. For example:
 class HandSynchedV2 {          
 private double state_ = 0.0; 
 private final Sync lock;  // use lock type supplied in constructor
 public HandSynchedV2(Sync l) { lock = l; } 
 protected void runSafely(Runnable r) {
 try {
 lock.acquire();
 try { r.run(); }
 finally { lock.release(); }
 }
 catch (InterruptedException ex) { // propagate without throwing
 Thread.currentThread().interrupt();
 }
 }
 public void changeState(double d) {
 runSafely(new Runnable() {
 public void run() { state_ = updateFunction(d); } 
 });
 }
 // ...
 }
 

One reason to bother with such constructions is to use deadlock- avoiding back-offs when dealing with locks involving multiple objects. For example, here is a Cell class that uses attempt to back-off and retry if two Cells are trying to swap values with each other at the same time.

 class Cell {
 long value;
 Sync lock = ... // some sync implementation class
 void swapValue(Cell other) {
 for (;;) { 
 try {
 lock.acquire();
 try {
 if (other.lock.attempt(100)) {
 try { 
 long t = value; 
 value = other.value;
 other.value = t;
 return;
 }
 finally { other.lock.release(); }
 }
 }
 finally { lock.release(); }
 } 
 catch (InterruptedException ex) { return; }
 }
 }
 }
 

Here is an even fancier version, that uses lock re-ordering upon conflict:

 class Cell { 
 long value;
 Sync lock = ...;
 private static boolean trySwap(Cell a, Cell b) {
 a.lock.acquire();
 try {
 if (!b.lock.attempt(0)) 
 return false;
 try { 
 long t = a.value;
 a.value = b.value;
 b.value = t;
 return true;
 }
 finally { other.lock.release(); }
 }
 finally { lock.release(); }
 return false;
 }
 void swapValue(Cell other) {
 try {
 while (!trySwap(this, other) &&
 !tryswap(other, this)) 
 Thread.sleep(1);
 }
 catch (InterruptedException ex) { return; }
 }
 }
 

Interruptions are in general handled as early as possible. Normally, InterruptionExceptions are thrown in acquire and attempt(msec) if interruption is detected upon entry to the method, as well as in any later context surrounding waits. However, interruption status is ignored in release();

Timed versions of attempt report failure via return value. If so desired, you can transform such constructions to use exception throws via

 if (!c.attempt(timeval)) throw new TimeoutException(timeval);
 

The TimoutSync wrapper class can be used to automate such usages.

All time values are expressed in milliseconds as longs, which have a maximum value of Long.MAX_VALUE, or almost 300,000 centuries. It is not known whether JVMs actually deal correctly with such extreme values. For convenience, some useful time values are defined as static constants.

All implementations of the three Sync methods guarantee to somehow employ Java synchronized methods or blocks, and so entail the memory operations described in JLS chapter 17 which ensure that variables are loaded and flushed within before/after constructions.

Syncs may also be used in spinlock constructions. Although it is normally best to just use acquire(), various forms of busy waits can be implemented. For a simple example (but one that would probably never be preferable to using acquire()):

 class X {
 Sync lock = ...
 void spinUntilAcquired() throws InterruptedException {
 // Two phase. 
 // First spin without pausing.
 int purespins = 10; 
 for (int i = 0; i < purespins; ++i) {
 if (lock.attempt(0))
 return true;
 }
 // Second phase - use timed waits
 long waitTime = 1; // 1 millisecond
 for (;;) {
 if (lock.attempt(waitTime))
 return true;
 else 
 waitTime = waitTime * 3 / 2 + 1; // increase 50% 
 }
 }
 }
 

In addition pure synchronization control, Syncs may be useful in any context requiring before/after methods. For example, you can use an ObservableSync (perhaps as part of a LayeredSync) in order to obtain callbacks before and after each method invocation for a given class.

[ Introduction to this package. ]



Field Summary
final public static  longONE_CENTURY
    
final public static  longONE_DAY
    
final public static  longONE_HOUR
    
final public static  longONE_MINUTE
    
final public static  longONE_SECOND
    
final public static  longONE_WEEK
    
final public static  longONE_YEAR
    


Method Summary
public  voidacquire()
     Wait (possibly forever) until successful passage. Fail only upon interuption.
public  booleanattempt(long msecs)
     Wait at most msecs to pass; report whether passed.

The method has best-effort semantics: The msecs bound cannot be guaranteed to be a precise upper bound on wait time in Java. Implementations generally can only attempt to return as soon as possible after the specified bound.

public  voidrelease()
     Potentially enable others to pass.

Because release does not raise exceptions, it can be used in `finally' clauses without requiring extra embedded try/catch blocks.


Field Detail
ONE_CENTURY
final public static long ONE_CENTURY(Code)
One century in milliseconds; convenient as a time-out value *



ONE_DAY
final public static long ONE_DAY(Code)
One day, in milliseconds; convenient as a time-out value *



ONE_HOUR
final public static long ONE_HOUR(Code)
One hour, in milliseconds; convenient as a time-out value *



ONE_MINUTE
final public static long ONE_MINUTE(Code)
One minute, in milliseconds; convenient as a time-out value *



ONE_SECOND
final public static long ONE_SECOND(Code)
One second, in milliseconds; convenient as a time-out value *



ONE_WEEK
final public static long ONE_WEEK(Code)
One week, in milliseconds; convenient as a time-out value *



ONE_YEAR
final public static long ONE_YEAR(Code)
One year in milliseconds; convenient as a time-out value *





Method Detail
acquire
public void acquire() throws InterruptedException(Code)
Wait (possibly forever) until successful passage. Fail only upon interuption. Interruptions always result in `clean' failures. On failure, you can be sure that it has not been acquired, and that no corresponding release should be performed. Conversely, a normal return guarantees that the acquire was successful.



attempt
public boolean attempt(long msecs) throws InterruptedException(Code)
Wait at most msecs to pass; report whether passed.

The method has best-effort semantics: The msecs bound cannot be guaranteed to be a precise upper bound on wait time in Java. Implementations generally can only attempt to return as soon as possible after the specified bound. Also, timers in Java do not stop during garbage collection, so timeouts can occur just because a GC intervened. So, msecs arguments should be used in a coarse-grained manner. Further, implementations cannot always guarantee that this method will return at all without blocking indefinitely when used in unintended ways. For example, deadlocks may be encountered when called in an unintended context.


Parameters:
  msecs - the number of milleseconds to wait.An argument less than or equal to zero means not to wait at all. However, this may still requireaccess to a synchronization lock, which can impose unboundeddelay if there is a lot of contention among threads. true if acquired




release
public void release()(Code)
Potentially enable others to pass.

Because release does not raise exceptions, it can be used in `finally' clauses without requiring extra embedded try/catch blocks. But keep in mind that as with any java method, implementations may still throw unchecked exceptions such as Error or NullPointerException when faced with uncontinuable errors. However, these should normally only be caught by higher-level error handlers.




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