lock方法

  public static void main(String[] args) throws InterruptedException {
    Object lock = new Object();
    AtomicInteger integer = new AtomicInteger(0);
    CompletableFuture.runAsync(()->{
      synchronized (lock)
      {
        while (integer.getAndIncrement()<100)
        {
          System.out.println("a"+"  "+integer.get());
          try {
            lock.notify();
            lock.wait();
          }catch (InterruptedException e)
          {
            throw new RuntimeException(e);
          }
        }
      }
    });
    CompletableFuture.runAsync(()->{
      synchronized (lock)
      {
        while (integer.getAndIncrement()<100)
        {
          System.out.println("b"+"  "+integer.get());
          try {
            lock.notify();
            lock.wait();
          }catch (InterruptedException e)
          {
            throw new RuntimeException(e);
          }
        }
      }
    });
    Thread.currentThread().join();
  }

这个方法有点问题,就是说打印到最后,不会自动结束,主线程会一直停在这里,不会自动退出

CyclicBarrier方法

    public static void main(String[] args) throws InterruptedException {
        CyclicBarrier barrier = new CyclicBarrier(2);
        AtomicInteger integer = new AtomicInteger();
        System.out.println(integer.get());
        CompletableFuture.runAsync(() -> {
            try {
                while (integer.incrementAndGet() < 100) {
                    System.out.println("a" + "  " + integer.get());
                    barrier.await();
                    barrier.await();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        CompletableFuture.runAsync(() -> {
            try {
                while (integer.incrementAndGet() < 100) {
                    System.out.println("b" + "  " + integer.get());
                    barrier.await();
                    // System.out.println("b"+"  "+integer.get());
                    barrier.await();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        Thread.currentThread().join();
    }

这个方法有点问题,就是说打印到最后,不会自动结束,主线程会一直停在这里,不会自动退出

CountDownLatch方法

    public static void main(String[] args) throws InterruptedException {
      AtomicReference<CountDownLatch> countA = new AtomicReference<>(new CountDownLatch(0));
      AtomicReference<CountDownLatch> countB = new AtomicReference<>(new CountDownLatch(1));
      new Thread(()->{
        for (int i = 0; i <50 ; i++) {
          try {
            countA.get().await();
            System.out.println("a"+"  "+i);
            countA.set(new CountDownLatch(1));
            countB.get().countDown();
          } catch (InterruptedException e) {
            throw new RuntimeException(e);
          }
        }
      }).start();

      new Thread(()->{
        for (int i = 0; i <50 ; i++) {
          try {
            countB.get().await();
            System.out.println("b"+"  "+i);
            countB.set(new CountDownLatch(1));
            countA.get().countDown();
          } catch (InterruptedException e) {
            throw new RuntimeException(e);
          }
        }
      }).start();
    }
  • CountDownLatch 为0 就是表示可以线程可以直接运行的
  • await方法 当CountDownLatch中count为0的时候,可以直接运行
  • 运用上面这个两个机制就可以保证a先运行,b后执行,然后ab交替执行了,很妙

Semaphore信号量方法

 private static Semaphore semaphoreA = new Semaphore(1);
    private static Semaphore semaphoreB = new Semaphore(0);
    private static int count = 0;

    public static void main(String[] args) {
        Thread threadA = new Thread(() -> {
            while (count < 100) {
                try {
                    System.out.println("a  " + semaphoreA.availablePermits());
                    semaphoreA.acquire();
                    System.out.println("a_after  " + semaphoreA.availablePermits());
                    System.out.println("a  " + count);
                    count++;
                    System.out.println("b  " + semaphoreB.availablePermits());
                    semaphoreB.release();
                    System.out.println("b_after " + semaphoreB.availablePermits());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread threadB = new Thread(() -> {
            while (count < 100) {
                try {
                    semaphoreB.acquire();
                    System.out.println("b  " + count);
                    count++;
                    semaphoreA.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        threadA.start();
        threadB.start();

        try {
            threadA.join();
            threadB.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
  • availablePermits方法是用来获取许可证数量的
  • 是通过初始值的信号量设置加上acquire 和release方法来实现a b 交替打印的