test/compiler/testlibrary/rtm/BusyLock.java

Print this page




  60     public void run() {
  61         try {
  62             // wait until forceAbort leave monitor
  63             barrier.await();
  64             if (UNSAFE.tryMonitorEnter(monitor)) {
  65                 try {
  66                     barrier.await();
  67                     Thread.sleep(timeout);
  68                 } finally {
  69                     UNSAFE.monitorExit(monitor);
  70                 }
  71             } else {
  72                 throw new RuntimeException("Monitor should be entered by " +
  73                                            "::run() first.");
  74             }
  75         } catch (InterruptedException | BrokenBarrierException e) {
  76             throw new RuntimeException("Synchronization error happened.", e);
  77         }
  78     }
  79 
  80     public void test() {
  81         try {
  82             barrier.await();
  83             // wait until monitor is locked by a ::run method
  84             barrier.await();
  85         } catch (InterruptedException | BrokenBarrierException e) {
  86             throw new RuntimeException("Synchronization error happened.", e);
  87         }




  88         synchronized(monitor) {
  89             BusyLock.field++;
  90         }
  91     }
  92 
  93     @Override
  94     public String getMethodWithLockName() {
  95         return this.getClass().getName() + "::test";
  96     }
  97 
  98     @Override
  99     public String[] getMethodsToCompileNames() {
 100         return new String[] { getMethodWithLockName() };
 101     }
 102 
 103     /**
 104      * Usage:
 105      * BusyLock [ <inflate monitor> [ <timeout> ] ]
 106      *
 107      * Default values are:


 113     public static void main(String args[]) throws Exception {
 114         int timeoutValue = BusyLock.DEFAULT_TIMEOUT;
 115         boolean inflateMonitor = true;
 116 
 117         if (args.length > 0 ) {
 118             inflateMonitor = Boolean.valueOf(args[0]);
 119 
 120             if (args.length > 1) {
 121                 timeoutValue = Integer.valueOf(args[1]);
 122             }
 123         }
 124 
 125         BusyLock busyLock = new BusyLock(timeoutValue);
 126 
 127         if (inflateMonitor) {
 128             AbortProvoker.inflateMonitor(busyLock.monitor);
 129         }
 130 
 131         Thread t = new Thread(busyLock);
 132         t.start();
 133         busyLock.test();
 134         t.join();
 135     }
 136 }


  60     public void run() {
  61         try {
  62             // wait until forceAbort leave monitor
  63             barrier.await();
  64             if (UNSAFE.tryMonitorEnter(monitor)) {
  65                 try {
  66                     barrier.await();
  67                     Thread.sleep(timeout);
  68                 } finally {
  69                     UNSAFE.monitorExit(monitor);
  70                 }
  71             } else {
  72                 throw new RuntimeException("Monitor should be entered by " +
  73                                            "::run() first.");
  74             }
  75         } catch (InterruptedException | BrokenBarrierException e) {
  76             throw new RuntimeException("Synchronization error happened.", e);
  77         }
  78     }
  79 
  80     public void syncAndTest() {
  81         try {
  82             barrier.await();
  83             // wait until monitor is locked by a ::run method
  84             barrier.await();
  85         } catch (InterruptedException | BrokenBarrierException e) {
  86             throw new RuntimeException("Synchronization error happened.", e);
  87         }
  88         test();
  89     }
  90 
  91     public void test() {
  92         synchronized(monitor) {
  93             BusyLock.field++;
  94         }
  95     }
  96 
  97     @Override
  98     public String getMethodWithLockName() {
  99         return this.getClass().getName() + "::test";
 100     }
 101 
 102     @Override
 103     public String[] getMethodsToCompileNames() {
 104         return new String[] { getMethodWithLockName() };
 105     }
 106 
 107     /**
 108      * Usage:
 109      * BusyLock [ <inflate monitor> [ <timeout> ] ]
 110      *
 111      * Default values are:


 117     public static void main(String args[]) throws Exception {
 118         int timeoutValue = BusyLock.DEFAULT_TIMEOUT;
 119         boolean inflateMonitor = true;
 120 
 121         if (args.length > 0 ) {
 122             inflateMonitor = Boolean.valueOf(args[0]);
 123 
 124             if (args.length > 1) {
 125                 timeoutValue = Integer.valueOf(args[1]);
 126             }
 127         }
 128 
 129         BusyLock busyLock = new BusyLock(timeoutValue);
 130 
 131         if (inflateMonitor) {
 132             AbortProvoker.inflateMonitor(busyLock.monitor);
 133         }
 134 
 135         Thread t = new Thread(busyLock);
 136         t.start();
 137         busyLock.syncAndTest();
 138         t.join();
 139     }
 140 }