java.lang.Thread.sleep()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(4.5k)|赞(0)|评价(0)|浏览(302)

本文整理了Java中java.lang.Thread.sleep()方法的一些代码示例,展示了Thread.sleep()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Thread.sleep()方法的具体详情如下:
包路径:java.lang.Thread
类名称:Thread
方法名:sleep

Thread.sleep介绍

[英]Causes the thread which sent this message to sleep for the given interval of time (given in milliseconds). The precision is not guaranteed - the Thread may sleep more or less than requested.
[中]使发送此消息的线程在给定的时间间隔(以毫秒为单位)内休眠。精度无法保证-线程可能会比要求的睡眠时间长或短。

代码示例

代码示例来源:origin: iluwatar/java-design-patterns

private static void artificialDelayOf(long millis) {
 try {
  Thread.sleep(millis);
 } catch (InterruptedException e) {
  LOGGER.error("sleep interrupted", e);
 }
}

代码示例来源:origin: iluwatar/java-design-patterns

private static long ap(long i) {
  try {
   Thread.sleep(i);
  } catch (InterruptedException e) {
   LOGGER.error("Exception caught.", e);
  }
  return i * (i + 1) / 2;
 }
}

代码示例来源:origin: iluwatar/java-design-patterns

/**
  * Simulate the write operation
  */
 public void write() throws InterruptedException {
  LOGGER.info("{} begin", name);
  Thread.sleep(writingTime);
  LOGGER.info("{} finished after writing {}ms", name, writingTime);
 }
}

代码示例来源:origin: iluwatar/java-design-patterns

/**
  * Simulate the read operation
  * 
  */
 public void read() throws InterruptedException {
  LOGGER.info("{} begin", name);
  Thread.sleep(readingTime);
  LOGGER.info("{} finish after reading {}ms", name, readingTime);
 }
}

代码示例来源:origin: iluwatar/java-design-patterns

/**
 * Constructor
 */
public Oliphaunt() {
 id = counter.incrementAndGet();
 try {
  Thread.sleep(1000);
 } catch (InterruptedException e) {
  e.printStackTrace();
 }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public Integer apply(Integer t) {
  if (c++ <= 0) {
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
    }
  }
  return t;
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void accept(Integer v) throws Exception {
    if (v == 1) {
      Thread.sleep(100);
    }
  }
})

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Object call() throws Exception {
    Thread.sleep(50);
    return null;
  }
}).timeout(100, TimeUnit.MILLISECONDS, normal.completable);

代码示例来源:origin: ReactiveX/RxJava

@Override
public Integer apply(Integer t) {
  if (c++ <= 0) {
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
    }
  }
  return t;
}

代码示例来源:origin: iluwatar/java-design-patterns

@Override
 public void run() {
  LOGGER.info("{} processing {}", Thread.currentThread().getName(), task.toString());
  try {
   Thread.sleep(task.getTimeMs());
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public Object get() throws InterruptedException, ExecutionException {
  Thread.sleep(500);
  isDone.compareAndSet(false, true);
  return "foo";
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void run() throws Exception {
    try {
    Thread.sleep(3000);
    } catch (InterruptedException ex) {
      interrupted.set(true);
    }
  }
})

代码示例来源:origin: ReactiveX/RxJava

@Override
public Integer apply(Integer t1) {
  if (c++ <= 1) {
    // slow
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
  return t1;
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public String apply(Long t1) {
  System.out.println(t1);
  try {
    Thread.sleep(100);
  } catch (InterruptedException e) {
  }
  return t1 + " slow value";
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public void accept(Integer t1) {
  if (count.incrementAndGet() == 500000) {
    // give it a small break halfway through
    try {
      Thread.sleep(50);
    } catch (InterruptedException ex) {
      // ignored
    }
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public void accept(String t1) {
  try {
    // simulate a slow observer
    Thread.sleep(50);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  value1.set(t1);
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public void onNext(Integer t) {
  // Consume after sleep for 1 ms
  try {
    Thread.sleep(1);
  } catch (InterruptedException e) {
    // ignored
  }
  if (counter.getAndIncrement() % 100 == 0) {
    System.out.println("testIssue2890NoStackoverflow -> " + counter.get());
  };
}

代码示例来源:origin: ReactiveX/RxJava

void beforeCancelSleep(BaseTestConsumer<?, ?> ts) throws Exception {
  long before = System.currentTimeMillis();
  Thread.sleep(50);
  if (System.currentTimeMillis() - before > 100) {
    ts.dispose();
    throw new IllegalStateException("Overslept?" + (System.currentTimeMillis() - before));
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void run() {
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      // ignore
    }
    o.onNext(value);
  }
}.start();

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testUnsubscribeOnNestedTakeAndSyncInfiniteStream() throws InterruptedException {
  final AtomicInteger subscribeCounter = new AtomicInteger();
  final AtomicInteger sentEventCounter = new AtomicInteger();
  doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(SYNC_INFINITE_OBSERVABLE_OF_EVENT(2, subscribeCounter, sentEventCounter), subscribeCounter);
  Thread.sleep(500);
  assertEquals(39, sentEventCounter.get());
}

相关文章