public static long copyInputStreamToOutputStream( InputStream inputstream, OutputStream outputstream, int i, long l) throws IOException, InterruptedException, InvalidParameter { ThreadUtil.checkIfInterrupted(); long l1 = 0L; byte abyte0[] = new byte[i]; do { label0: { i = inputstream.read(abyte0, 0, abyte0.length); long l2 = l1; if (i > 0) { l1 += i; if (l1 <= l) { break label0; } l2 = l1; } return l2; } ThreadUtil.checkIfInterrupted(); outputstream.write(abyte0, 0, i); ThreadUtil.checkIfInterrupted(); } while (true); }
@Test public void interruptAndJoinThreadsTrueOnNull() throws Exception { assertTrue(ThreadUtil.interruptAndJoinThreads((Thread[]) null)); assertTrue(ThreadUtil.interruptAndJoinThreads((Thread[]) null, 100)); assertTrue(ThreadUtil.interruptAndJoinThreads((Collection<Thread>) null)); assertTrue(ThreadUtil.interruptAndJoinThreads((Collection<Thread>) null, 100)); }
@Test(timeOut = 1000) public void interruptAndJoinWontStopOnFirstInterrupt() throws Exception { final CountDownLatch started = new CountDownLatch(1); final CountDownLatch stopped = new CountDownLatch(1); Thread t = new Thread(new InterruptTestRunnable(started, stopped, 1)); t.start(); started.await(); // give the other thread some time to enter sleep Thread.sleep(50); assertFalse(ThreadUtil.interruptAndJoin(t, 50)); assertTrue(ThreadUtil.interruptAndJoin(t, 50)); // the thread is no longer alive, successive calls must always return true assertFalse(t.isAlive()); for (int i = 0; i < 10; i++) { assertTrue(ThreadUtil.interruptAndJoin(t)); } }
public static void showSafe(final CharSequence text) { ThreadUtil.runInUIThread( new Runnable() { @Override public void run() { show(text); } }); }
public static final synchronized Configure getInstance() { if (instance == null) { instance = new Configure(); instance.setDaemon(true); instance.setName(ThreadUtil.getName(instance)); instance.start(); } return instance; }
@Test public void interruptAndJoinThreads() throws Exception { final int N = 10; final CountDownLatch started = new CountDownLatch(N); final CountDownLatch stopped = new CountDownLatch(N); List<Thread> ts = new ArrayList<>(); for (int i = 0; i < N; i++) { Thread t = new Thread(new InterruptTestRunnable(started, stopped, 1)); ts.add(t); } ThreadUtil.startThreads(ts); started.await(); // give the other threads some time to enter sleep Thread.sleep(50); assertFalse(ThreadUtil.interruptAndJoinThreads(ts, 25)); assertTrue(ThreadUtil.interruptAndJoinThreads(ts, 25)); for (Thread t : ts) { assertFalse(t.isAlive()); } }
@Test public void sleepUntilRegular() throws InterruptedException { long start = System.currentTimeMillis(); for (int i = 1; i <= 100; i++) { ThreadUtil.sleepUntilMs(start + i); } long elapsed = System.currentTimeMillis() - start; assertTrue(elapsed >= 100); // this is probably also flaky as hell due to different scheduling behaviour of different // platforms. lets see how well it does. assertTrue(elapsed <= 125); }
@Test public void sleepUntilMsIntoThePast() throws InterruptedException { // 1000 times no sleep at all long tStart = System.nanoTime(); for (int i = 0; i < 1000; i++) { long tms = System.currentTimeMillis(); ThreadUtil.sleepUntilMs(tms - 10); // noop } long elapsed = (System.nanoTime() - tStart); // this should finish within a few hundred microseconds // but that would make the test very flaky due to os-specific scheduling assertTrue(elapsed < 25_000_1000); // 25ms }
public void run() { Logger.println("Version " + Version.getAgentFullVersion()); long dateUnit = DateUtil.getDateUnit(); while (running) { reload(false); // Text Data Reset.. long nowUnit = DateUtil.getDateUnit(); if (dateUnit != nowUnit) { dateUnit = nowUnit; DataProxy.reset(); } ThreadUtil.sleep(3000); } }
void _open() throws IOException { long sleepTime = 100; final long start = System.currentTimeMillis(); while (true) { IOException lastError = null; try { _sock = SocketChannel.open(); _socket = _sock.socket(); _socket.connect(_addr, _options.connectTimeout); _socket.setTcpNoDelay(!USE_NAGLE); _socket.setSoTimeout(_options.socketTimeout); _in = _socket.getInputStream(); return; } catch (IOException ioe) { // TODO - erh to fix lastError = new IOException( "couldn't connect to [" + // _addr + "] bc:" + lastError , lastError ); lastError = new IOException("couldn't connect to [" + _addr + "] bc:" + ioe); _logger.log(Level.INFO, "connect fail to : " + _addr, ioe); } if (!_options.autoConnectRetry || (_pool != null && !_pool._everWorked)) throw lastError; long sleptSoFar = System.currentTimeMillis() - start; if (sleptSoFar >= CONN_RETRY_TIME_MS) throw lastError; if (sleepTime + sleptSoFar > CONN_RETRY_TIME_MS) sleepTime = CONN_RETRY_TIME_MS - sleptSoFar; _logger.severe( "going to sleep and retry. total sleep time after = " + (sleptSoFar + sleptSoFar) + "ms this time:" + sleepTime + "ms"); ThreadUtil.sleep(sleepTime); sleepTime *= 2; } }
public TempFiles() { scheduler = Executors.newScheduledThreadPool(1, ThreadUtil.getDaemonThreadFactory("tempfiles")); }
@Test public void interruptAndJoinTrueOnNull() throws Exception { assertTrue(ThreadUtil.interruptAndJoin(null)); assertTrue(ThreadUtil.interruptAndJoin(null, 100)); }
/** @deprecated Use {@link ThreadUtil#getActiveBgTaskCount()} instead */ @Deprecated public static int getActiveCount() { return ThreadUtil.getActiveBgTaskCount(); }
/** @deprecated Use {@link ThreadUtil#bgRunWait(Runnable)} instead */ @Deprecated public static void bgRunWait(Runnable runnable) { ThreadUtil.bgRunWait(runnable); }
/** @deprecated Use {@link ThreadUtil#bgRun(Runnable)} instead */ @Deprecated public static boolean bgRun(Runnable runnable) { return ThreadUtil.bgRun(runnable); }
/** @deprecated Use {@link ThreadUtil#bgRun(Runnable, boolean)} instead */ @Deprecated public static boolean bgRun(Runnable runnable, boolean onEventThread) { return ThreadUtil.bgRun(runnable, onEventThread); }