KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > execution > TaskThreadGroupGCTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.execution;
21
22 import java.awt.Frame JavaDoc;
23 import java.lang.ref.Reference JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28 import org.netbeans.junit.NbTestCase;
29 import org.openide.execution.ExecutionEngine;
30 import org.openide.execution.ExecutorTask;
31
32 /**
33  * Test that a task thread group is cleared when it is done.
34  * @see "#36395"
35  * @author Jesse Glick
36  */

37 public class TaskThreadGroupGCTest extends NbTestCase {
38     private Frame JavaDoc f;
39
40     public TaskThreadGroupGCTest(String JavaDoc name) {
41         super(name);
42     }
43
44     protected void setUp() throws Exception JavaDoc {
45         super.setUp();
46
47         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
48             public void run() {
49                 f = new Frame JavaDoc();
50                 f.setVisible(true);
51             }
52         });
53     }
54
55     protected void tearDown() throws Exception JavaDoc {
56
57         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
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 JavaDoc {
74         final List JavaDoc<Reference JavaDoc<Thread JavaDoc>> t = new ArrayList JavaDoc<Reference JavaDoc<Thread JavaDoc>>();
75         Runnable JavaDoc r = new Runnable JavaDoc() {
76             public void run() {
77                 System.out.println("Running a task in the execution engine...");
78                 t.add(new WeakReference JavaDoc<Thread JavaDoc>(Thread.currentThread()));
79                 Runnable JavaDoc r1 = new Runnable JavaDoc() {
80                     public void run() {
81                         System.out.println("Ran second thread.");
82                     }
83                 };
84                 Thread JavaDoc nue1 = new Thread JavaDoc(r1);
85                 nue1.start();
86                 try {
87                     nue1.join();
88                 } catch (InterruptedException JavaDoc e) {
89                     e.printStackTrace();
90                 }
91                 t.add(new WeakReference JavaDoc<Thread JavaDoc>(nue1));
92                 Runnable JavaDoc r2 = new Runnable JavaDoc() {
93                     public void run() {
94                         System.out.println("Ran third thread.");
95                     }
96                 };
97                 Thread JavaDoc nue2 = new Thread JavaDoc(r2);
98                 nue2.start();
99                 t.add(new WeakReference JavaDoc<Thread JavaDoc>(nue2));
100                 Runnable JavaDoc r3 = new Runnable JavaDoc() {
101                     public void run() {
102                         fail("Should not have even run.");
103                     }
104                 };
105                 Thread JavaDoc nue3 = new Thread JavaDoc(r3);
106                 t.add(new WeakReference JavaDoc<Thread JavaDoc>(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         /*
119         Thread main = t.get(0).get();
120         if (main != null) {
121             assertFalse(main.isAlive());
122             main = null;
123         }
124          */

125         assertGC("Collected task thread " + t.get(0).get(), t.get(0));
126     }
127
128 }
129
Popular Tags