1 /* 2 * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @library /test/lib 27 * @run junit/othervm -Djdk.trackAllThreads=true TrackAllThreads 28 */ 29 30 import java.lang.ref.WeakReference; 31 import java.util.concurrent.Executors; 32 import java.util.concurrent.LinkedTransferQueue; 33 import java.util.concurrent.ThreadFactory; 34 import java.util.concurrent.ThreadPoolExecutor; 35 import java.util.concurrent.TimeUnit; 36 import java.util.concurrent.locks.LockSupport; 37 38 import jdk.test.lib.util.ForceGC; 39 import org.junit.jupiter.api.Disabled; 40 import org.junit.jupiter.api.Test; 41 import static org.junit.jupiter.api.Assertions.*; 42 43 class TrackAllThreads { 44 45 /** 46 * Test that a virtual created directly with the Thread API, then parks, is not GC'ed. 47 */ 48 @Test 49 void testRootContainer() { 50 Thread thread = Thread.ofVirtual().start(LockSupport::park); 51 var ref = new WeakReference<>(thread); 52 thread = null; 53 ForceGC.waitFor(() -> ref.refersTo(null), 2000); 54 thread = ref.get(); 55 if (thread == null) { 56 fail("Thread has been GC'ed"); 57 } else { 58 LockSupport.unpark(thread); 59 } 60 } 61 62 /** 63 * Test that a ThreadPoolExecutor using a virtual thread factory can be GC'ed after 64 * the last virtual thread terminates. 65 */ 66 @Test 67 void testSharedContainer1() { 68 var queue = new LinkedTransferQueue<Runnable>(); 69 ThreadFactory factory = Thread.ofVirtual().factory(); 70 var executor = new ThreadPoolExecutor(0, 1, 1, TimeUnit.SECONDS, queue, factory); 71 executor.submit(() -> { }); 72 var ref = new WeakReference<>(executor); 73 executor = null; 74 boolean cleared = ForceGC.waitFor(() -> ref.refersTo(null), Long.MAX_VALUE); 75 assertTrue(cleared); 76 } 77 78 /** 79 * Test that a ThreadPoolExecutor using a virtual thread factory is not GC'ed when 80 * a task (running in a virtual thread) has parked. 81 */ 82 @Disabled 83 @Test 84 void testSharedContainer2() throws Exception { 85 var queue = new LinkedTransferQueue<Runnable>(); 86 ThreadFactory factory = Thread.ofVirtual().factory(); 87 var executor = new ThreadPoolExecutor(0, 1, 1, TimeUnit.SECONDS, queue, factory); 88 executor.execute(LockSupport::park); 89 var ref = new WeakReference<>(executor); 90 executor = null; 91 ForceGC.waitFor(() -> ref.refersTo(null), 2000); 92 executor = ref.get(); 93 if (executor == null) { 94 fail("Executor has been GC'ed"); 95 } else { 96 executor.shutdownNow(); 97 executor.close(); 98 } 99 } 100 101 /** 102 * Test that a ThreadPerTaskExecutor using a virtual thread factory can be GC'ed after 103 * the last virtual thread terminates. 104 */ 105 @Test 106 void testThreadPerTaskExecutor1() { 107 var executor = Executors.newVirtualThreadPerTaskExecutor(); 108 executor.submit(() -> { }); 109 var ref = new WeakReference<>(executor); 110 executor = null; 111 boolean cleared = ForceGC.waitFor(() -> ref.refersTo(null), Long.MAX_VALUE); 112 assertTrue(cleared); 113 } 114 115 /** 116 * Test that a ThreadPerTaskExecutor using a virtual thread factory is not GC'ed when 117 * a task (running in a virtual thread) has parked. 118 */ 119 @Disabled 120 @Test 121 void testThreadPerTaskExecutor2() throws Exception { 122 var executor = Executors.newVirtualThreadPerTaskExecutor(); 123 executor.execute(LockSupport::park); 124 var ref = new WeakReference<>(executor); 125 executor = null; 126 ForceGC.waitFor(() -> ref.refersTo(null), 2000); 127 executor = ref.get(); 128 if (executor == null) { 129 fail("Executor has been GC'ed"); 130 } else { 131 executor.shutdownNow(); 132 executor.close(); 133 } 134 } 135 }