test/java/lang/ref/EarlyTimeout.java

Print this page




  46 
  47 public class EarlyTimeout extends Thread {
  48 
  49     static final int THREADS_COUNT = 2;
  50     static final int TIMEOUT = 1000;
  51 
  52     static Object referent = new Object();
  53     static final ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
  54     static final WeakReference<Object> weakReference = new WeakReference<Object>(referent, queue);
  55     static final CountDownLatch startedSignal = new CountDownLatch(THREADS_COUNT);
  56 
  57     long actual;
  58     Reference<?> reference;
  59 
  60     public static void main(String[] args) throws Exception {
  61         EarlyTimeout[] threads = new EarlyTimeout[THREADS_COUNT];
  62         for (int i = 0; i < THREADS_COUNT; ++i) {
  63             threads[i] = new EarlyTimeout();
  64             threads[i].start();
  65         }
  66         startedSignal.await();
  67         referent = null;
  68         System.gc();

  69         for (EarlyTimeout thread : threads) {
  70             thread.join();
  71         }
  72         if (weakReference.get() != null) {
  73             throw new RuntimeException("weakReference was not cleared");
  74         }
  75         int nonNullRefCount = 0;
  76         for (EarlyTimeout thread : threads) {
  77             if (thread.reference == null && thread.actual < TIMEOUT) {
  78                 throw new RuntimeException("elapsed time " + thread.actual
  79                         + " is less than timeout " + TIMEOUT);
  80             }
  81             if (thread.reference != null && thread.reference == weakReference) {
  82                 nonNullRefCount++;
  83             }
  84         }
  85         if (nonNullRefCount != 1) {



  86             throw new RuntimeException("more than one references were removed from queue");
  87         }
  88     }
  89 
  90     public void run() {
  91         try {
  92             startedSignal.countDown();
  93             long start = System.currentTimeMillis();
  94             reference = queue.remove(TIMEOUT);
  95             actual = System.currentTimeMillis() - start;
  96         } catch (InterruptedException ex) {
  97             throw new RuntimeException(ex);
  98         }
  99     }
 100 }


  46 
  47 public class EarlyTimeout extends Thread {
  48 
  49     static final int THREADS_COUNT = 2;
  50     static final int TIMEOUT = 1000;
  51 
  52     static Object referent = new Object();
  53     static final ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
  54     static final WeakReference<Object> weakReference = new WeakReference<Object>(referent, queue);
  55     static final CountDownLatch startedSignal = new CountDownLatch(THREADS_COUNT);
  56 
  57     long actual;
  58     Reference<?> reference;
  59 
  60     public static void main(String[] args) throws Exception {
  61         EarlyTimeout[] threads = new EarlyTimeout[THREADS_COUNT];
  62         for (int i = 0; i < THREADS_COUNT; ++i) {
  63             threads[i] = new EarlyTimeout();
  64             threads[i].start();
  65         }

  66         referent = null;
  67         System.gc();
  68         startedSignal.await();
  69         for (EarlyTimeout thread : threads) {
  70             thread.join();
  71         }
  72         if (weakReference.get() != null) {
  73             throw new RuntimeException("weakReference was not cleared");
  74         }
  75         int nonNullRefCount = 0;
  76         for (EarlyTimeout thread : threads) {
  77             if (thread.reference == null && thread.actual < TIMEOUT) {
  78                 throw new RuntimeException("elapsed time " + thread.actual
  79                         + " is less than timeout " + TIMEOUT);
  80             }
  81             if (thread.reference != null && thread.reference == weakReference) {
  82                 nonNullRefCount++;
  83             }
  84         }
  85         if (nonNullRefCount == 0) {
  86             System.err.println("no references were removed from queue");
  87         }
  88         if (nonNullRefCount > 1) {
  89             throw new RuntimeException("more than one references were removed from queue");
  90         }
  91     }
  92 
  93     public void run() {
  94         try {
  95             startedSignal.countDown();
  96             long start = System.currentTimeMillis();
  97             reference = queue.remove(TIMEOUT);
  98             actual = System.currentTimeMillis() - start;
  99         } catch (InterruptedException ex) {
 100             throw new RuntimeException(ex);
 101         }
 102     }
 103 }