Java并发包详解

发布时间:2023-05-19

一、volatile关键字

volatile关键字用于保证变量在多线程之间的可见性。通常情况下,线程之间是互相不可见的,也就是说一个线程对变量的修改,其他线程不会立即感知到。使用volatile声明的变量可以保证当一个线程修改了该变量的值之后,其他线程能够立即感知到这个变化。

public class VolatileTest {
   private volatile int count = 0;
   public void increment() {
       count++;
   }
   public static void main(String[] args) throws InterruptedException {
       final VolatileTest test = new VolatileTest();
       for (int i = 0; i < 5; i++) {
         new Thread(() -> {
           for (int j = 0; j < 1000; j++) {
             test.increment();
           }
         }).start();
       }
       Thread.sleep(3000);
       System.out.println(test.count);
   }
}

上述代码可以看出,由于count变量被volatile修饰,所以多个线程对该变量的修改是可见的,最终输出的count变量的值是5000,而不是一个小于5000的数值。

二、synchronized关键字

synchronized关键字是Java中最基本的同步关键字,它用于保证多个线程之间的互斥访问,允许在同一时刻只有一个线程访问共享资源。

public class SynchronizedTest {
   private int count = 0;
   public synchronized void increment() {
       count++;
   }
   public static void main(String[] args) throws InterruptedException {
       final SynchronizedTest test = new SynchronizedTest();
       for (int i = 0; i < 5; i++) {
         new Thread(() -> {
           for (int j = 0; j < 1000; j++) {
             test.increment();
           }
         }).start();
       }
       Thread.sleep(3000);
       System.out.println(test.count);
   }
}

上述代码中,count变量被synchronized关键字修饰的increment方法访问。由于synchronized保证了同一时刻只有一个线程可以进入该方法,所以最终输出的count变量的值是5000。

三、ReentrantLock类

ReentrantLock类是Java中可重入锁的一种实现方式,也可以实现多个线程之间的互斥访问。与synchronized关键字不同的是,ReentrantLock类可以更加精细地控制锁的获取和释放,同时还支持公平锁和非公平锁。

public class ReentrantLockTest {
   private ReentrantLock lock = new ReentrantLock();
   private int count = 0;
   public void increment() {
       lock.lock();
       try {
           count++;
       } finally {
           lock.unlock();
       }
   }
   public static void main(String[] args) throws InterruptedException {
       final ReentrantLockTest test = new ReentrantLockTest();
       for (int i = 0; i < 5; i++) {
         new Thread(() -> {
           for (int j = 0; j < 1000; j++) {
             test.increment();
           }
         }).start();
       }
       Thread.sleep(3000);
       System.out.println(test.count);
   }
}

上述代码中,ReentrantLock类实现了多个线程之间的互斥访问。在increment方法中,首先需要调用lock方法获取锁,保证同一时刻只有一个线程可以修改count变量的值,最后需要调用unlock方法释放锁。最终输出的count变量的值是5000。

四、Condition接口

Condition接口是在Java 5中引入的,它提供了类似于Object的wait和notify方法的功能,但可以选择性地通知某些线程。与使用synchronized关键字和Object的wait方法不同,使用Condition对象可以在等待信号时释放锁。

public class ConditionTest {
   private ReentrantLock lock = new ReentrantLock();
   private Condition condition = lock.newCondition();
   private int count = 0;
   public void increment() {
       lock.lock();
       try {
           count++;
           condition.signalAll();
       } finally {
           lock.unlock();
       }
   }
   public void waitForCount() throws InterruptedException {
       lock.lock();
       try {
           while (count < 5000) {
               condition.await();
           }
       } finally {
           lock.unlock();
       }
   }
   public static void main(String[] args) throws InterruptedException {
       final ConditionTest test = new ConditionTest();
       new Thread(() -> {
           try {
               test.waitForCount();
               System.out.println("count has reached 5000");
           } catch (InterruptedException ex) {
               // ...
           }
       }).start();
       for (int i = 0; i < 5; i++) {
         new Thread(() -> {
           for (int j = 0; j < 1000; j++) {
             test.increment();
           }
         }).start();
       }
       Thread.sleep(3000);
   }
}

上述代码中,ConditionTest类中的waitForCount方法会线程等待,直到count变量的值达到5000。在increment方法中,除了对count变量进行修改之外,还会调用signalAll方法通知所有等待线程。最终输出的是“count has reached 5000”。

五、ReadWriteLock类

除了ReentrantLock之外,Java并发包中还提供了ReadWriteLock类,它可以使得多个线程同时读取共享变量,但是在有线程写入时,所有读线程会被阻塞。

public class ReadWriteLockTest {
   private int count = 0;
   private ReadWriteLock lock = new ReentrantReadWriteLock();
   public void increment() {
       lock.writeLock().lock();
       try {
           count++;
       } finally {
           lock.writeLock().unlock();
       }
   }
   public void printCount() {
       lock.readLock().lock();
       try {
           System.out.println(count);
       } finally {
           lock.readLock().unlock();
       }
   }
   public static void main(String[] args) throws InterruptedException {
       final ReadWriteLockTest test = new ReadWriteLockTest();
       for (int i = 0; i < 5; i++) {
         new Thread(() -> {
           for (int j = 0; j < 1000; j++) {
             test.increment();
           }
         }).start();
       }
       for (int i = 0; i < 5; i++) {
         new Thread(() -> {
           for (int j = 0; j < 1000; j++) {
             test.printCount();
           }
         }).start();
       }
       Thread.sleep(3000);
   }
}

上述代码中,increment方法使用写锁进行同步,而printCount方法使用读锁进行同步。由于读锁是共享锁,多个线程可以同时获得对共享变量的访问权。最终输出的count变量的值会大于等于5000。