public static void main(String[] args) { final Lock lock = new ReentrantLock(); final Condition condition = lock.newCondition(); class WaitThread extends Thread { String token; public WaitThread(String token) { super(); this.token = token; } @Override public void run() { try { System.out.println("Wait Thread " + token + " was started."); lock.lock(); System.out.println("Wait Thread " + token + " got the lock."); System.out.println("Wait Thread " + token + " start to wait."); condition.await(); System.out.println("Wait Thread " + token + " finish waiting."); System.out.println("Wait Thread " + token + " was finished."); } catch (InterruptedException e) { System.out.println("Wait Thread " + token + " was interrupted."); } } } final WaitThread waitThread = new WaitThread("one"); Thread modifyThread = new Thread() { @Override public void run() { try { System.out.println("Signal Thread was started."); TimeUnit.SECONDS.sleep(1L); boolean acquire = lock.tryLock(); System.out.println("Signal Thread got the lock: " + acquire); condition.signalAll(); lock.unlock(); System.out.println("Signal Thread was finished."); } catch (InterruptedException e) { e.printStackTrace(); } } }; waitThread.start(); modifyThread.start(); }
@Test public void test() { final GammaTxnLong ref = new GammaTxnLong(stm); WaitThread t = new WaitThread(ref); t.start(); sleepMs(1000); assertAlive(t); stm.getDefaultTxnExecutor() .execute( new TxnVoidCallable() { @Override public void call(Txn tx) throws Exception { GammaTxn btx = (GammaTxn) tx; Tranlocal write = ref.openForWrite(btx, LOCKMODE_NONE); write.long_value = 1; } }); joinAll(t); assertEquals(2, ref.atomicGet()); }
public void run(String[] command, String directory, String caption, boolean keepWindow) throws IOException { this.setTitle(caption); this.keepWindow = keepWindow; btnCancel.setText("Cancel"); returnCode = -1; canceled = false; setModal(true); ProcessBuilder pb = new ProcessBuilder(command); pb.directory(new File(directory)); process = pb.start(); waitThread = new WaitThread(); waitThread.start(); txtStdout.setText(""); txtStderr.setText(""); // BufferedReader kozo = new BufferedReader ( new InputStreamReader( process.getInputStream()), // 1); // stdoutThread = new StreamReadThread(kozo, txtStdout); // kozo = new BufferedReader ( new InputStreamReader( process.getErrorStream()), 1); // stderrThread = new StreamReadThread(kozo, txtStderr); stdoutThread = new StreamReadThread(process.getInputStream(), txtStdout); stderrThread = new StreamReadThread(process.getErrorStream(), txtStderr); stdoutThread.start(); stderrThread.start(); setVisible(true); }