public static void main(String[] args) {
        AtomicInteger a = new AtomicInteger(0);
        CountDownLatch countDownLatch = new CountDownLatch(1);
        Semaphore semaphore = new Semaphore(1);
        for (int i = 0; i < 10; i++) {
            Thread t1 = new Thread(() -> {
                for (; ; ) {try {
                        semaphore.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            int ret=a.get();
if (ret == 1000) { countDownLatch.countDown(); break; } else { ret = a.incrementAndGet(); System.out.println(ret); } semaphore.release(); } }); t1.setDaemon(true); t1.start(); } try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } }

 

相关文章:

  • 2021-07-13
  • 2021-12-11
  • 2022-12-23
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-27
  • 2021-11-19
  • 2022-02-11
  • 2022-12-23
  • 2021-07-28
相关资源
相似解决方案