< prev index next >

test/jdk/java/lang/ref/OOMEInReferenceHandler.java

Print this page

 65              )
 66              ;
 67 
 68          Thread[] threads = new Thread[tg.activeCount()];
 69          Thread referenceHandlerThread = null;
 70          int n = tg.enumerate(threads);
 71          for (int i = 0; i < n; i++) {
 72              if ("Reference Handler".equals(threads[i].getName())) {
 73                  referenceHandlerThread = threads[i];
 74              }
 75          }
 76 
 77          if (referenceHandlerThread == null) {
 78              throw new IllegalStateException("Couldn't find Reference Handler thread.");
 79          }
 80 
 81          ReferenceQueue<Object> refQueue = new ReferenceQueue<>();
 82          Object referent = new Object();
 83          WeakReference<Object> weakRef = new WeakReference<>(referent, refQueue);
 84 


 85          Object waste = fillHeap();
 86 
 87          referenceHandlerThread.interrupt();
 88 
 89          // allow referenceHandlerThread some time to throw OOME
 90          Thread.sleep(500L);
 91 
 92          // release waste & referent
 93          waste = null;
 94          referent = null;
 95 
 96          // wait at most 10 seconds for success or failure
 97          for (int i = 0; i < 20; i++) {
 98              if (refQueue.poll() != null) {
 99                  // Reference Handler thread still working -> success
100                  return;
101              }
102              System.gc();
103              Thread.sleep(500L); // wait a little to allow GC to do it's work before allocating objects
104              if (!referenceHandlerThread.isAlive()) {

 65              )
 66              ;
 67 
 68          Thread[] threads = new Thread[tg.activeCount()];
 69          Thread referenceHandlerThread = null;
 70          int n = tg.enumerate(threads);
 71          for (int i = 0; i < n; i++) {
 72              if ("Reference Handler".equals(threads[i].getName())) {
 73                  referenceHandlerThread = threads[i];
 74              }
 75          }
 76 
 77          if (referenceHandlerThread == null) {
 78              throw new IllegalStateException("Couldn't find Reference Handler thread.");
 79          }
 80 
 81          ReferenceQueue<Object> refQueue = new ReferenceQueue<>();
 82          Object referent = new Object();
 83          WeakReference<Object> weakRef = new WeakReference<>(referent, refQueue);
 84 
 85          // Allow thread.sleep() to pre-allocate ThreadSleepEvent object
 86          Thread.sleep(1L);
 87          Object waste = fillHeap();
 88 
 89          referenceHandlerThread.interrupt();
 90 
 91          // allow referenceHandlerThread some time to throw OOME
 92          Thread.sleep(500L);
 93 
 94          // release waste & referent
 95          waste = null;
 96          referent = null;
 97 
 98          // wait at most 10 seconds for success or failure
 99          for (int i = 0; i < 20; i++) {
100              if (refQueue.poll() != null) {
101                  // Reference Handler thread still working -> success
102                  return;
103              }
104              System.gc();
105              Thread.sleep(500L); // wait a little to allow GC to do it's work before allocating objects
106              if (!referenceHandlerThread.isAlive()) {
< prev index next >