1 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 35 public final class Background { 36 37 private static boolean loaded = false; 38 private static boolean loadfailed = false; 39 40 private Thread peer; 41 private Cancellable cancel; 42 43 private Background(Thread peer, Cancellable c) { 44 this.peer = peer; 45 cancel = c; 46 } 47 48 public static Background execute(Runnable run) { 49 Cancellable cancel = (Cancellable) (run instanceof Cancellable ? run : null); 50 if (useHack()) { 51 Thread t = new Thread (new Wrapper(run), "Background"); 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(); peer.interrupt(); 68 } 69 if (cancel != null) cancel.cancel(); 70 } 71 72 private static boolean useHack() { 77 78 String os = System.getProperty("os.name"); if ("Linux".equals(os) == false) return false; 81 String osversion = "" + System.getProperty("os.version"); if (osversion.startsWith("2.4") == false) return false; 85 String vendor = "" + System.getProperty("java.vm.vendor"); if (vendor.startsWith("Sun") == false) return false; 88 String version = "" + System.getProperty("java.vm.version"); if (version.startsWith("1.4") == false) return false; 91 String hw = System.getProperty("os.arch"); if ("i386".equals(hw) == false) return false; 94 loadLibrary(); 95 return loaded; 96 } 97 98 private static class Wrapper implements Runnable { 99 100 private final Runnable peer; 101 102 public Wrapper(Runnable run) { 103 this.peer = run; 104 } 105 106 public void run() { 107 native_nice(); 108 peer.run(); 109 } 110 111 } 112 113 116 private static class ThreadExtractor implements Runnable { 117 118 private final Runnable peer; 119 private Thread thread; 120 121 ThreadExtractor(Runnable run) { 122 peer = run; 123 } 124 125 public void run() { 126 Thread.currentThread().interrupted(); synchronized (this) { 128 thread = Thread.currentThread(); 129 notifyAll(); 130 } 131 peer.run(); 132 } 133 134 public synchronized Thread getThread() { 135 while (thread == null) { 136 try { 137 wait(); 138 } catch (InterruptedException e) { 139 } 141 } 142 return thread; 143 } 144 } 145 146 148 private static void loadLibrary() { 149 if (loadfailed) return; 150 if (false == loaded) { 151 try { 152 System.loadLibrary("tasklist_bgthreads"); loaded = true; 155 } catch (Throwable t) { 156 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, t); 157 loadfailed = true; 158 } 159 } 160 } 161 162 163 166 private static native void native_nice(); 167 168 171 public static void main(String [] args) throws Exception { 172 if (useHack() == true) { 173 native_nice(); 174 System.out.println("I'm niced for 1 minute. Check it by top utility."); Thread.sleep(60*1000); 176 } 177 } 178 } 179 | Popular Tags |