changeset 4798:6fbd69f8e3ab

7094995: Trailing daemon thread causes continuous GC in agentvm mode Summary: Shutdown GcInducingThread once test (successfully) finishes Reviewed-by: alanb, chegar, dholmes, darcy Contributed-by: Neil Richards <neil.richards@ngmr.net>
author ngmr
date Fri, 18 Nov 2011 09:03:43 +0000
parents 955aae8c1106
children cf47846165f4
files test/java/util/zip/ZipFile/ClearStaleZipFileInputStreams.java
diffstat 1 files changed, 41 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/util/zip/ZipFile/ClearStaleZipFileInputStreams.java	Thu Nov 24 11:34:31 2011 +0000
+++ b/test/java/util/zip/ZipFile/ClearStaleZipFileInputStreams.java	Fri Nov 18 09:03:43 2011 +0000
@@ -63,11 +63,9 @@
             File.createTempFile("test-data" + compression, ".zip");
         tempZipFile.deleteOnExit();
 
-        ZipOutputStream zos =
-            new ZipOutputStream(new FileOutputStream(tempZipFile));
-        zos.setLevel(compression);
-
-        try {
+        try (FileOutputStream fos = new FileOutputStream(tempZipFile);
+                ZipOutputStream zos = new ZipOutputStream(fos)) {
+            zos.setLevel(compression);
             for (int i = 0; i < ZIP_ENTRY_NUM; i++) {
                 String text = "Entry" + i;
                 ZipEntry entry = new ZipEntry(text);
@@ -78,33 +76,47 @@
                     zos.closeEntry();
                 }
             }
-        } finally {
-            zos.close();
         }
 
         return tempZipFile;
     }
 
-    private static void startGcInducingThread(final int sleepMillis) {
-        final Thread gcInducingThread = new Thread() {
-            public void run() {
-                while (true) {
-                    System.gc();
-                    try {
-                        Thread.sleep(sleepMillis);
-                    } catch (InterruptedException e) { }
+    private static final class GcInducingThread extends Thread {
+        private final int sleepMillis;
+        private boolean keepRunning = true;
+
+        public GcInducingThread(final int sleepMillis) {
+            this.sleepMillis = sleepMillis;
+        }
+
+        public synchronized void run() {
+            while (keepRunning) {
+                System.gc();
+                try {
+                    wait(sleepMillis);
+                } catch (InterruptedException e) {
+                    System.out.println("GCing thread unexpectedly interrupted");
+                    return;
                 }
             }
-        };
+        }
 
-        gcInducingThread.setDaemon(true);
-        gcInducingThread.start();
+        public synchronized void shutDown() {
+            keepRunning = false;
+            notifyAll();
+        }
     }
 
     public static void main(String[] args) throws Exception {
-        startGcInducingThread(500);
-        runTest(ZipOutputStream.DEFLATED);
-        runTest(ZipOutputStream.STORED);
+        GcInducingThread gcThread = new GcInducingThread(500);
+        gcThread.start();
+        try {
+            runTest(ZipOutputStream.DEFLATED);
+            runTest(ZipOutputStream.STORED);
+        } finally {
+            gcThread.shutDown();
+            gcThread.join();
+        }
     }
 
     private static void runTest(int compression) throws Exception {
@@ -113,21 +125,16 @@
         System.out.println("Testing with a zip file with compression level = "
                 + compression);
         File f = createTestFile(compression);
-        try {
-            ZipFile zf = new ZipFile(f);
-            try {
-                Set<Object> refSet = createTransientInputStreams(zf, rq);
+        try (ZipFile zf = new ZipFile(f)) {
+            Set<Object> refSet = createTransientInputStreams(zf, rq);
 
-                System.out.println("Waiting for 'stale' input streams from ZipFile to be GC'd ...");
-                System.out.println("(The test will hang on failure)");
-                while (false == refSet.isEmpty()) {
-                    refSet.remove(rq.remove());
-                }
-                System.out.println("Test PASSED.");
-                System.out.println();
-            } finally {
-                zf.close();
+            System.out.println("Waiting for 'stale' input streams from ZipFile to be GC'd ...");
+            System.out.println("(The test will hang on failure)");
+            while (false == refSet.isEmpty()) {
+                refSet.remove(rq.remove());
             }
+            System.out.println("Test PASSED.");
+            System.out.println();
         } finally {
             f.delete();
         }