⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sync.html

📁 dfdfddfskfjdsklfjksdljflksjfsjlkfdjlksfjkdsjfsdjkflsjkf
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.4.2_03) on Mon May 31 21:03:11 EDT 2004 -->
<TITLE>
Sync (DbUnit 2.1 API)
</TITLE>

<META NAME="keywords" CONTENT="org.dbunit.util.concurrent.Sync interface">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../stylesheet.css" TITLE="Style">

<SCRIPT type="text/javascript">
function windowTitle()
{
    parent.document.title="Sync (DbUnit 2.1 API)";
}
</SCRIPT>

</HEAD>

<BODY BGCOLOR="white" onload="windowTitle();">

<!-- ========= START OF TOP NAVBAR ======= -->
<A NAME="navbar_top"><!-- --></A><A HREF="#skip-navbar_top" title="Skip navigation links"></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
<TR>
<TD COLSPAN=3 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
  <TR ALIGN="center" VALIGN="top">
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="class-use/Sync.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>
  </TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
</EM>
</TD>
</TR>

<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
&nbsp;<A HREF="../../../../org/dbunit/util/concurrent/Slot.html" title="class in org.dbunit.util.concurrent"><B>PREV CLASS</B></A>&nbsp;
&nbsp;<A HREF="../../../../org/dbunit/util/concurrent/SynchronizedInt.html" title="class in org.dbunit.util.concurrent"><B>NEXT CLASS</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
  <A HREF="../../../../index.html" target="_top"><B>FRAMES</B></A>  &nbsp;
&nbsp;<A HREF="Sync.html" target="_top"><B>NO FRAMES</B></A>  &nbsp;
&nbsp;<SCRIPT type="text/javascript">
  <!--
  if(window==top) {
    document.writeln('<A HREF="../../../../allclasses-noframe.html"><B>All Classes</B></A>');
  }
  //-->
</SCRIPT>
<NOSCRIPT>
  <A HREF="../../../../allclasses-noframe.html"><B>All Classes</B></A>
</NOSCRIPT>
</FONT></TD>
</TR>
<TR>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
  SUMMARY:&nbsp;NESTED&nbsp;|&nbsp;<A HREF="#field_summary">FIELD</A>&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL:&nbsp;<A HREF="#field_detail">FIELD</A>&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_top"></A><!-- ========= END OF TOP NAVBAR ========= -->

<HR>
<!-- ======== START OF CLASS DATA ======== -->
<H2>
<FONT SIZE="-1">
org.dbunit.util.concurrent</FONT>
<BR>
Interface Sync</H2>
<DL>
<DT><B>All Known Implementing Classes:</B> <DD><A HREF="../../../../org/dbunit/util/concurrent/Semaphore.html" title="class in org.dbunit.util.concurrent">Semaphore</A></DD>
</DL>
<HR>
<DL>
<DT>public interface <B>Sync</B></DL>

<P>
Main interface for locks, gates, and conditions. <p> 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. <p> Most Syncs are intended to be used primarily (although not exclusively) in  before/after constructions such as: <pre> 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     }   } } </pre> Syncs may be used in somewhat tedious but more flexible replacements for built-in Java synchronized blocks. For example: <pre> 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) { ... } } </pre> 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: <pre> 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); }      });   }   // ... } </pre> <p> 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. <pre> 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; }     }   } }</pre> <p> Here is an even fancier version, that uses lock re-ordering upon conflict: <pre> 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; }  }}</pre> <p> 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(); <p> Timed versions of attempt report failure via return value. If so desired, you can transform such constructions to use exception throws via  <pre>   if (!c.attempt(timeval)) throw new TimeoutException(timeval); </pre> <p> The TimoutSync wrapper class can be used to automate such usages. <p> 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. <p> All implementations of the three Sync methods guarantee to somehow employ Java <code>synchronized</code> 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. <p> 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()): <pre> 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%      }   } } </pre> <p> 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. <p> <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
<P>

<P>
<HR>

<P>
<!-- ======== NESTED CLASS SUMMARY ======== -->


<!-- =========== FIELD SUMMARY =========== -->

<A NAME="field_summary"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -