コード例 #1
0
 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);
 }
コード例 #2
0
ファイル: ThreadUtilTest.java プロジェクト: phicode/philib
 @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));
 }
コード例 #3
0
ファイル: ThreadUtilTest.java プロジェクト: phicode/philib
 @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));
   }
 }
コード例 #4
0
 public static void showSafe(final CharSequence text) {
   ThreadUtil.runInUIThread(
       new Runnable() {
         @Override
         public void run() {
           show(text);
         }
       });
 }
コード例 #5
0
ファイル: Configure.java プロジェクト: sugiii/scouter
 public static final synchronized Configure getInstance() {
   if (instance == null) {
     instance = new Configure();
     instance.setDaemon(true);
     instance.setName(ThreadUtil.getName(instance));
     instance.start();
   }
   return instance;
 }
コード例 #6
0
ファイル: ThreadUtilTest.java プロジェクト: phicode/philib
 @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());
   }
 }
コード例 #7
0
ファイル: ThreadUtilTest.java プロジェクト: phicode/philib
 @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);
 }
コード例 #8
0
ファイル: ThreadUtilTest.java プロジェクト: phicode/philib
 @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
 }
コード例 #9
0
ファイル: Configure.java プロジェクト: sugiii/scouter
 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);
   }
 }
コード例 #10
0
ファイル: DBPort.java プロジェクト: eapfel/mongo-java-driver
  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;
    }
  }
コード例 #11
0
 public TempFiles() {
   scheduler = Executors.newScheduledThreadPool(1, ThreadUtil.getDaemonThreadFactory("tempfiles"));
 }
コード例 #12
0
ファイル: ThreadUtilTest.java プロジェクト: phicode/philib
 @Test
 public void interruptAndJoinTrueOnNull() throws Exception {
   assertTrue(ThreadUtil.interruptAndJoin(null));
   assertTrue(ThreadUtil.interruptAndJoin(null, 100));
 }
コード例 #13
0
 /** @deprecated Use {@link ThreadUtil#getActiveBgTaskCount()} instead */
 @Deprecated
 public static int getActiveCount() {
   return ThreadUtil.getActiveBgTaskCount();
 }
コード例 #14
0
 /** @deprecated Use {@link ThreadUtil#bgRunWait(Runnable)} instead */
 @Deprecated
 public static void bgRunWait(Runnable runnable) {
   ThreadUtil.bgRunWait(runnable);
 }
コード例 #15
0
 /** @deprecated Use {@link ThreadUtil#bgRun(Runnable)} instead */
 @Deprecated
 public static boolean bgRun(Runnable runnable) {
   return ThreadUtil.bgRun(runnable);
 }
コード例 #16
0
 /** @deprecated Use {@link ThreadUtil#bgRun(Runnable, boolean)} instead */
 @Deprecated
 public static boolean bgRun(Runnable runnable, boolean onEventThread) {
   return ThreadUtil.bgRun(runnable, onEventThread);
 }