KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > Background


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.modules.tasklist.core;
21
22 import org.openide.util.RequestProcessor;
23 import org.openide.util.Cancellable;
24 import org.openide.ErrorManager;
25
26 /**
27  * Workarounds RequestProcessor (and JVM) background threads behaviour
28  * on Linux systems that map thread to OS scheduled processes.
29  * <p>
30  * Do not forget to run JVM with -Djava.library.path=/home/pk97937/prj/academy/linux
31  * property or put into NetBeans search path (modules/bin).
32  *
33  * @author Petr Kuzel
34  */

35 public final class Background {
36
37     private static boolean loaded = false;
38     private static boolean loadfailed = false;
39
40     private Thread JavaDoc peer;
41     private Cancellable cancel;
42
43     private Background(Thread JavaDoc peer, Cancellable c) {
44         this.peer = peer;
45         cancel = c;
46     }
47
48     public static Background execute(Runnable JavaDoc run) {
49         Cancellable cancel = (Cancellable) (run instanceof Cancellable ? run : null);
50         if (useHack()) {
51             Thread JavaDoc t = new Thread JavaDoc(new Wrapper(run), "Background"); // NOI18N
52
t.setPriority(Thread.MIN_PRIORITY);
53             t.setDaemon(true);
54             t.start();
55
56             return new Background(t, cancel);
57         } else {
58             ThreadExtractor extractor = new ThreadExtractor(run);
59             RequestProcessor.getDefault().post(extractor, 0, Thread.MIN_PRIORITY);
60             return new Background(extractor.getThread(), cancel);
61         }
62     }
63
64     public final void interrupt() {
65         if (peer != null) {
66             peer.interrupt(); // it's not enough see #38399
67
peer.interrupt();
68         }
69         if (cancel != null) cancel.cancel();
70     }
71
72     // use hack on linux JVM with successfuly loaded library
73
// it works with Sun provided Linux 1.4 series JVMs on i386
74
// feel free to weaken vendor and version rules if you find
75
// other JVM that maps Java threads to linux processes
76
private static boolean useHack() {
77
78         String JavaDoc os = System.getProperty("os.name"); // NOI18N
79
if ("Linux".equals(os) == false) return false; // NOI18N
80

81         // jlahoda thinks that JVM threading is correct on 2.6.x kernels, he'll investigate
82
String JavaDoc osversion = "" + System.getProperty("os.version"); // NOI18N
83
if (osversion.startsWith("2.4") == false) return false; // NOI18N
84

85         String JavaDoc vendor = "" + System.getProperty("java.vm.vendor"); // NOI18N
86
if (vendor.startsWith("Sun") == false) return false; // NOI18N
87

88         String JavaDoc version = "" + System.getProperty("java.vm.version"); // NOI18N
89
if (version.startsWith("1.4") == false) return false; // NOI18N
90

91         String JavaDoc hw = System.getProperty("os.arch"); // NOI18N
92
if ("i386".equals(hw) == false) return false; // NOI18N
93

94         loadLibrary();
95         return loaded;
96     }
97
98     private static class Wrapper implements Runnable JavaDoc {
99
100         private final Runnable JavaDoc peer;
101
102         public Wrapper(Runnable JavaDoc run) {
103             this.peer = run;
104         }
105
106         public void run() {
107             native_nice();
108             peer.run();
109         }
110
111     }
112
113     /**
114      * Get actual RP thread for given Runnable.
115      */

116     private static class ThreadExtractor implements Runnable JavaDoc {
117
118         private final Runnable JavaDoc peer;
119         private Thread JavaDoc thread;
120
121         ThreadExtractor(Runnable JavaDoc run) {
122             peer = run;
123         }
124
125         public void run() {
126             Thread.currentThread().interrupted(); // consume/clear the flag
127
synchronized (this) {
128                 thread = Thread.currentThread();
129                 notifyAll();
130             }
131             peer.run();
132         }
133
134         public synchronized Thread JavaDoc getThread() {
135             while (thread == null) {
136                 try {
137                     wait();
138                 } catch (InterruptedException JavaDoc e) {
139                     // null thread
140
}
141             }
142             return thread;
143         }
144     }
145
146     // JNI related section ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147

148     private static void loadLibrary() {
149         if (loadfailed) return;
150         if (false == loaded) {
151             try {
152                 // XXX be aware of #32080, that changes location of native libraries
153
System.loadLibrary("tasklist_bgthreads"); // NOI18N
154
loaded = true;
155             } catch (Throwable JavaDoc t) {
156                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, t);
157                 loadfailed = true;
158             }
159         }
160     }
161
162
163     /**
164      * Nice current process (on some JVM implementations Java thread).
165      */

166     private static native void native_nice();
167     
168     /**
169      * Simple self test.
170      */

171     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
172         if (useHack() == true) {
173             native_nice();
174             System.out.println("I'm niced for 1 minute. Check it by top utility."); // NOI18N
175
Thread.sleep(60*1000);
176         }
177     }
178 }
179
Popular Tags