1 19 20 package org.netbeans.core.execution; 21 22 import java.awt.Frame ; 23 import java.lang.ref.Reference ; 24 import java.lang.ref.WeakReference ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import javax.swing.SwingUtilities ; 28 import org.netbeans.junit.NbTestCase; 29 import org.openide.execution.ExecutionEngine; 30 import org.openide.execution.ExecutorTask; 31 32 37 public class TaskThreadGroupGCTest extends NbTestCase { 38 private Frame f; 39 40 public TaskThreadGroupGCTest(String name) { 41 super(name); 42 } 43 44 protected void setUp() throws Exception { 45 super.setUp(); 46 47 SwingUtilities.invokeAndWait(new Runnable () { 48 public void run() { 49 f = new Frame (); 50 f.setVisible(true); 51 } 52 }); 53 } 54 55 protected void tearDown() throws Exception { 56 57 SwingUtilities.invokeAndWait(new Runnable () { 58 public void run() { 59 f.setVisible(false); 60 f.dispose(); 61 f = null; 62 } 63 }); 64 super.tearDown(); 65 } 66 67 68 protected int timeOut() { 69 return 60000; 70 } 71 72 73 public void testTTGGC() throws Exception { 74 final List <Reference <Thread >> t = new ArrayList <Reference <Thread >>(); 75 Runnable r = new Runnable () { 76 public void run() { 77 System.out.println("Running a task in the execution engine..."); 78 t.add(new WeakReference <Thread >(Thread.currentThread())); 79 Runnable r1 = new Runnable () { 80 public void run() { 81 System.out.println("Ran second thread."); 82 } 83 }; 84 Thread nue1 = new Thread (r1); 85 nue1.start(); 86 try { 87 nue1.join(); 88 } catch (InterruptedException e) { 89 e.printStackTrace(); 90 } 91 t.add(new WeakReference <Thread >(nue1)); 92 Runnable r2 = new Runnable () { 93 public void run() { 94 System.out.println("Ran third thread."); 95 } 96 }; 97 Thread nue2 = new Thread (r2); 98 nue2.start(); 99 t.add(new WeakReference <Thread >(nue2)); 100 Runnable r3 = new Runnable () { 101 public void run() { 102 fail("Should not have even run."); 103 } 104 }; 105 Thread nue3 = new Thread (r3); 106 t.add(new WeakReference <Thread >(nue3)); 107 System.out.println("done."); 108 } 109 }; 110 ExecutorTask task = ExecutionEngine.getDefault().execute("foo", r, null); 111 assertEquals(0, task.result()); 112 assertFalse(t.toString(), t.contains(null)); 113 r = null; 114 task = null; 115 assertGC("Collected secondary task thread too", t.get(1)); 116 assertGC("Collected forked task thread too", t.get(2)); 117 assertGC("Collected unstarted task thread too", t.get(3)); 118 125 assertGC("Collected task thread " + t.get(0).get(), t.get(0)); 126 } 127 128 } 129 | Popular Tags |