Home > Java > javaTutorial > body text

Java 8 Concurrency Tutorial: Synchronization and Locks

黄舟
Release: 2017-02-07 10:17:17
Original
1446 people have browsed it

Welcome to the second part of my Java8 concurrency tutorial. This guide will teach you how to program concurrently in Java 8 with simple and easy-to-understand code examples. This is the second in a series of tutorials. In the next 15 minutes, you'll learn how to synchronize access to shared mutable variables through the synchronization keyword, locks, and semaphores.

  • Part One: Threads and Executors

  • Part Two: Synchronization and Locks

  • Part Three: Atomic Operations and ConcurrentMap

The central concepts presented in this article also apply to older versions of Java, however the code examples are for Java 8 and rely heavily on lambda expressions and new concurrency features. If you are not familiar with lambdas yet, I recommend you read my Java 8 tutorial first.

For the sake of simplicity, the code examples in this tutorial use the two helper functions sleep(seconds) and stop(executor) defined here.

Synchronization

In the previous chapter, we learned how to execute code simultaneously through the executor service. When we write this kind of multi-threaded code, we need to pay special attention to concurrent access to shared mutable variables. Suppose we want to increment some integer that can be accessed by multiple threads simultaneously.

We define the count field with the increment() method to increase the count by one:

int count = 0;

void increment() {
    count = count + 1;
}
Copy after login

When multiple threads call this method concurrently, we will run into big trouble:

ExecutorService executor = Executors.newFixedThreadPool(2);

IntStream.range(0, 10000)
    .forEach(i -> executor.submit(this::increment));

stop(executor);

System.out.println(count);  // 9965
Copy after login


We did not see the result of count 10000, the actual result of the above code is different every time it is executed. The reason is that we are sharing mutable variables on different threads and there is no synchronization mechanism for variable access, which creates race conditions.

Increasing a value requires three steps: (1) Read the current value, (2) Add one to this value, (3) Write the new value to the variable. If two threads execute at the same time, it is possible that two threads execute step 1 at the same time, so the same current value will be read. This results in invalid writes, so the actual result will be smaller. In the above example, asynchronous concurrent access to count loses 35 increment operations, but you will see different results when you execute the code yourself.

Fortunately, Java has supported thread synchronization through the synchronized keyword since a long time ago. We can use synchronized to fix the above race condition when increasing count.

synchronized void incrementSync() {
    count = count + 1;
}
Copy after login

When we called incrementSync() concurrently, we got the expected result of count 10000. There are no more race conditions, and the results are stable across every code execution:

ExecutorService executor = Executors.newFixedThreadPool(2);

IntStream.range(0, 10000)
    .forEach(i -> executor.submit(this::incrementSync));

stop(executor);

System.out.println(count);  // 10000
Copy after login

The synchronized keyword can also be used in statement blocks:

void incrementSync() {
    synchronized (this) {
        count = count + 1;
    }
}
Copy after login

Java internally uses so-called "watches" "monitor" (monitor), also known as monitor lock (monitor lock) or intrinsic lock (intrinsic lock) to manage synchronization. Monitors are bound to objects, for example, when using synchronized methods, each method shares the same monitor of the corresponding object.

All implicit monitors implement the reentrant feature. Reentrancy means that the lock is bound to the current thread. A thread can safely acquire the same lock multiple times without deadlock (for example, a synchronized method calling another synchronized method of the same object).

Lock

The concurrency API supports a variety of explicit locks, which are specified by the Lock interface and are used to replace synchronized implicit locks. Locks support multiple methods for fine-grained control, so they have greater overhead than implicit monitors.

Multiple implementations of locks are provided in the standard JDK, and they will be shown in the following chapters.

ReentrantLock

The ReentrantLock class is a mutex lock with the same behavior as an implicit monitor accessed via synchronized, but with extended functionality. As its name suggests, this lock implements reentrancy properties, just like an implicit monitor.

Let's look at the above example after using ReentrantLock.

ReentrantLock lock = new ReentrantLock();
int count = 0;

void increment() {
    lock.lock();
    try {
        count++;
    } finally {
        lock.unlock();
    }
}
Copy after login

锁可以通过lock()来获取,通过unlock()来释放。把你的代码包装在try-finally代码块中来确保异常情况下的解锁非常重要。这个方法是线程安全的,就像同步副本那样。如果另一个线程已经拿到锁了,再次调用lock()会阻塞当前线程,直到锁被释放。在任意给定的时间内,只有一个线程可以拿到锁。

锁对细粒度的控制支持多种方法,就像下面的例子那样:

executor.submit(() -> {
    lock.lock();
    try {
        sleep(1);
    } finally {
        lock.unlock();
    }
});

executor.submit(() -> {
    System.out.println("Locked: " + lock.isLocked());
    System.out.println("Held by me: " + lock.isHeldByCurrentThread());
    boolean locked = lock.tryLock();
    System.out.println("Lock acquired: " + locked);
});

stop(executor);
Copy after login

在第一个任务拿到锁的一秒之后,第二个任务获得了锁的当前状态的不同信息。

Locked: true
Held by me: false
Lock acquired: false
Copy after login

tryLock()方法是lock()方法的替代,它尝试拿锁而不阻塞当前线程。在访问任何共享可变变量之前,必须使用布尔值结果来检查锁是否已经被获取。

ReadWriteLock

ReadWriteLock接口规定了锁的另一种类型,包含用于读写访问的一对锁。读写锁的理念是,只要没有任何线程写入变量,并发读取可变变量通常是安全的。所以读锁可以同时被多个线程持有,只要没有线程持有写锁。这样可以提升性能和吞吐量,因为读取比写入更加频繁。

ExecutorService executor = Executors.newFixedThreadPool(2);
Map<String, String> map = new HashMap<>();
ReadWriteLock lock = new ReentrantReadWriteLock();

executor.submit(() -> {
    lock.writeLock().lock();
    try {
        sleep(1);
        map.put("foo", "bar");
    } finally {
        lock.writeLock().unlock();
    }
});
Copy after login

上面的例子在暂停一秒之后,首先获取写锁来向映射添加新的值。在这个任务完成之前,两个其它的任务被启动,尝试读取映射中的元素,并暂停一秒:

Runnable readTask = () -> {
    lock.readLock().lock();
    try {
        System.out.println(map.get("foo"));
        sleep(1);
    } finally {
        lock.readLock().unlock();
    }
};

executor.submit(readTask);
executor.submit(readTask);

stop(executor);
Copy after login

当你执行这一代码示例时,你会注意到两个读任务需要等待写任务完成。在释放了写锁之后,两个读任务会同时执行,并同时打印结果。它们不需要相互等待完成,因为读锁可以安全同步获取,只要没有其它线程获取了写锁。

StampedLock

Java 8 自带了一种新的锁,叫做StampedLock,它同样支持读写锁,就像上面的例子那样。与ReadWriteLock不同的是,StampedLock的锁方法会返回表示为long的标记。你可以使用这些标记来释放锁,或者检查锁是否有效。此外,StampedLock支持另一种叫做乐观锁(optimistic locking)的模式。

让我们使用StampedLock代替ReadWriteLock重写上面的例子:

ExecutorService executor = Executors.newFixedThreadPool(2);
Map<String, String> map = new HashMap<>();
StampedLock lock = new StampedLock();

executor.submit(() -> {
    long stamp = lock.writeLock();
    try {
        sleep(1);
        map.put("foo", "bar");
    } finally {
        lock.unlockWrite(stamp);
    }
});

Runnable readTask = () -> {
    long stamp = lock.readLock();
    try {
        System.out.println(map.get("foo"));
        sleep(1);
    } finally {
        lock.unlockRead(stamp);
    }
};

executor.submit(readTask);
executor.submit(readTask);

stop(executor);
Copy after login

通过readLock() 或 writeLock()来获取读锁或写锁会返回一个标记,它可以在稍后用于在finally块中解锁。要记住StampedLock并没有实现重入特性。每次调用加锁都会返回一个新的标记,并且在没有可用的锁时阻塞,即使相同线程已经拿锁了。所以你需要额外注意不要出现死锁。

就像前面的ReadWriteLock例子那样,两个读任务都需要等待写锁释放。之后两个读任务同时向控制台打印信息,因为多个读操作不会相互阻塞,只要没有线程拿到写锁。

下面的例子展示了乐观锁:

ExecutorService executor = Executors.newFixedThreadPool(2);
StampedLock lock = new StampedLock();

executor.submit(() -> {
    long stamp = lock.tryOptimisticRead();
    try {
        System.out.println("Optimistic Lock Valid: " + lock.validate(stamp));
        sleep(1);
        System.out.println("Optimistic Lock Valid: " + lock.validate(stamp));
        sleep(2);
        System.out.println("Optimistic Lock Valid: " + lock.validate(stamp));
    } finally {
        lock.unlock(stamp);
    }
});

executor.submit(() -> {
    long stamp = lock.writeLock();
    try {
        System.out.println("Write Lock acquired");
        sleep(2);
    } finally {
        lock.unlock(stamp);
        System.out.println("Write done");
    }
});

stop(executor);
Copy after login

乐观的读锁通过调用tryOptimisticRead()获取,它总是返回一个标记而不阻塞当前线程,无论锁是否真正可用。如果已经有写锁被拿到,返回的标记等于0。你需要总是通过lock.validate(stamp)检查标记是否有效。

执行上面的代码会产生以下输出:

Optimistic Lock Valid: true
Write Lock acquired
Optimistic Lock Valid: false
Write done
Optimistic Lock Valid: false
Copy after login

乐观锁在刚刚拿到锁之后是有效的。和普通的读锁不同的是,乐观锁不阻止其他线程同时获取写锁。在第一个线程暂停一秒之后,第二个线程拿到写锁而无需等待乐观的读锁被释放。此时,乐观的读锁就不再有效了。甚至当写锁释放时,乐观的读锁还处于无效状态。

所以在使用乐观锁时,你需要每次在访问任何共享可变变量之后都要检查锁,来确保读锁仍然有效。

有时,将读锁转换为写锁而不用再次解锁和加锁十分实用。StampedLock为这种目的提供了tryConvertToWriteLock()方法,就像下面那样:

ExecutorService executor = Executors.newFixedThreadPool(2);
StampedLock lock = new StampedLock();

executor.submit(() -> {
    long stamp = lock.readLock();
    try {
        if (count == 0) {
            stamp = lock.tryConvertToWriteLock(stamp);
            if (stamp == 0L) {
                System.out.println("Could not convert to write lock");
                stamp = lock.writeLock();
            }
            count = 23;
        }
        System.out.println(count);
    } finally {
        lock.unlock(stamp);
    }
});

stop(executor);
Copy after login

第一个任务获取读锁,并向控制台打印count字段的当前值。但是如果当前值是零,我们希望将其赋值为23。我们首先需要将读锁转换为写锁,来避免打破其它线程潜在的并发访问。tryConvertToWriteLock()的调用不会阻塞,但是可能会返回为零的标记,表示当前没有可用的写锁。这种情况下,我们调用writeLock()来阻塞当前线程,直到有可用的写锁。

信号量

除了锁之外,并发 API 也支持计数的信号量。不过锁通常用于变量或资源的互斥访问,信号量可以维护整体的准入许可。这在一些不同场景下,例如你需要限制你程序某个部分的并发访问总数时非常实用。

下面是一个例子,演示了如何限制对通过sleep(5)模拟的长时间运行任务的访问:

ExecutorService executor = Executors.newFixedThreadPool(10);

Semaphore semaphore = new Semaphore(5);

Runnable longRunningTask = () -> {
    boolean permit = false;
    try {
        permit = semaphore.tryAcquire(1, TimeUnit.SECONDS);
        if (permit) {
            System.out.println("Semaphore acquired");
            sleep(5);
        } else {
            System.out.println("Could not acquire semaphore");
        }
    } catch (InterruptedException e) {
        throw new IllegalStateException(e);
    } finally {
        if (permit) {
            semaphore.release();
        }
    }
}

IntStream.range(0, 10)
    .forEach(i -> executor.submit(longRunningTask));

stop(executor);
Copy after login

执行器可能同时运行 10 个任务,但是我们使用了大小为5的信号量,所以将并发访问限制为5。使用try-finally代码块在异常情况中合理释放信号量十分重要。

执行上述代码产生如下结果:

Semaphore acquired
Semaphore acquired
Semaphore acquired
Semaphore acquired
Semaphore acquired
Could not acquire semaphore
Could not acquire semaphore
Could not acquire semaphore
Could not acquire semaphore
Could not acquire semaphore
Copy after login

信号量限制对通过sleep(5)模拟的长时间运行任务的访问,最大5个线程。每个随后的tryAcquire()调用在经过最大为一秒的等待超时之后,会向控制台打印不能获取信号量的结果。

这就是我的系列并发教程的第二部分。以后会放出更多的部分,所以敬请等待吧。像以前一样,你可以在Github上找到这篇文档的所有示例代码,所以请随意fork这个仓库,并自己尝试它。

我希望你能喜欢这篇文章。如果你还有任何问题,在下面的评论中向我反馈。你也可以在 Twitter 上关注我来获取更多开发相关的信息。

以上就是Java 8 并发教程:同步和锁的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!