Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 1st, 2012  |  syntax: None  |  size: 2.30 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import com.google.common.util.concurrent.Monitor;
  2.  
  3. import java.util.concurrent.atomic.AtomicInteger;
  4.  
  5. /**
  6.  * Created by IntelliJ IDEA.
  7.  * User: bbejeck
  8.  * Date: 11/11/11
  9.  * Time: 10:01 PM
  10.  */
  11. public class MonitorExample {
  12.  
  13.     private final Monitor monitor = new Monitor();
  14.     private volatile boolean condition = true;
  15.     private int taskDoneCounter;
  16.     private AtomicInteger taskSkippedCounter = new AtomicInteger(0);
  17.     private int stopTaskCount;
  18.  
  19.     private Monitor.Guard conditionGuard = new Monitor.Guard(monitor) {
  20.         @Override
  21.         public boolean isSatisfied() {
  22.             return condition;
  23.         }
  24.     };
  25.  
  26.     public void demoTryEnterIf() throws InterruptedException {
  27.         if (monitor.tryEnterIf(conditionGuard)) {
  28.             try {
  29.                 simulatedWork();
  30.                 taskDoneCounter++;
  31.             } finally {
  32.                 monitor.leave();
  33.             }
  34.         } else {
  35.             taskSkippedCounter.incrementAndGet();
  36.         }
  37.     }
  38.  
  39.     public void demoEnterIf() throws InterruptedException {
  40.         if (monitor.enterIf(conditionGuard)) {
  41.             try {
  42.                 taskDoneCounter++;
  43.                 if (taskDoneCounter == stopTaskCount) {
  44.                     condition = false;
  45.                 }
  46.             } finally {
  47.                 monitor.leave();
  48.             }
  49.         } else {
  50.             taskSkippedCounter.incrementAndGet();
  51.         }
  52.  
  53.     }
  54.  
  55.     public void demoEnterWhen() throws InterruptedException {
  56.         monitor.enterWhen(conditionGuard);
  57.         try {
  58.             taskDoneCounter++;
  59.             if (taskDoneCounter == stopTaskCount) {
  60.                 condition = false;
  61.             }
  62.         } finally {
  63.             monitor.leave();
  64.         }
  65.     }
  66.  
  67.     private void simulatedWork() throws InterruptedException{
  68.         Thread.sleep(250);
  69.     }
  70.  
  71.     public void reEvaluateGuardCondition() {
  72.         monitor.reevaluateGuards();
  73.     }
  74.  
  75.     public int getStopTaskCount() {
  76.         return stopTaskCount;
  77.     }
  78.  
  79.     public void setStopTaskCount(int stopTaskCount) {
  80.         this.stopTaskCount = stopTaskCount;
  81.     }
  82.  
  83.     public void setCondition(boolean condition) {
  84.         this.condition = condition;
  85.     }
  86.  
  87.     public int getTaskSkippedCounter() {
  88.         return taskSkippedCounter.get();
  89.     }
  90.  
  91.     public int getTaskDoneCounter() {
  92.         return taskDoneCounter;
  93.     }
  94. }