1 32 package net.sf.retrotranslator.runtime.java.util.concurrent.locks; 33 34 import net.sf.retrotranslator.runtime.impl.WeakIdentityTable; 35 36 39 public class LockSupport_ { 40 41 private static final WeakIdentityTable<Thread , ThreadLock> table = 42 new WeakIdentityTable<Thread , ThreadLock>() { 43 protected ThreadLock initialValue() { 44 return new ThreadLock(); 45 } 46 }; 47 48 private LockSupport_() { 49 } 50 51 private static class ThreadLock { 52 53 private boolean permit; 54 55 public ThreadLock() { 56 } 57 58 public synchronized void park() throws InterruptedException { 59 if (permit) { 60 permit = false; 61 } else { 62 wait(); 63 } 64 } 65 66 public synchronized void parkNanos(long nanos) throws InterruptedException { 67 if (permit) { 68 permit = false; 69 } else { 70 wait(nanos / 1000000, (int) (nanos % 1000000)); 71 } 72 } 73 74 public synchronized void parkUntil(long deadline) throws InterruptedException { 75 if (permit) { 76 permit = false; 77 } else { 78 long millis = deadline - System.currentTimeMillis(); 79 if (millis > 0) { 80 wait(millis); 81 } 82 } 83 } 84 85 public synchronized void unpark() { 86 if (!permit) { 87 permit = true; 88 notify(); 89 } 90 } 91 92 } 93 94 public static void park() { 95 try { 96 table.obtain(Thread.currentThread()).park(); 97 } catch (InterruptedException e) { 98 Thread.currentThread().interrupt(); 99 } 100 } 101 102 public static void parkNanos(long nanos) { 103 try { 104 table.obtain(Thread.currentThread()).parkNanos(nanos); 105 } catch (InterruptedException e) { 106 Thread.currentThread().interrupt(); 107 } 108 } 109 110 public static void parkUntil(long deadline) { 111 try { 112 table.obtain(Thread.currentThread()).parkUntil(deadline); 113 } catch (InterruptedException e) { 114 Thread.currentThread().interrupt(); 115 } 116 } 117 118 public static void unpark(Thread thread) { 119 table.obtain(thread).unpark(); 120 } 121 122 } 123 | Popular Tags |