The Java language is a language that introduced multi-threading in the early days. The use of threads makes the Java language shine in the concurrent processing of programs. However, synchronization issues and mutual exclusion issues between threads have always been the key to the programming process. In the Java language, there are many ways to implement thread synchronization and mutual exclusion. This article will introduce several of them.
1. Use the synchronized keyword to achieve synchronization and mutual exclusion
Synchronized is the most basic way to achieve synchronization and mutual exclusion in the Java language. In Java, each object has a monitor. When a thread enters the monitor's code block, it obtains the lock of the object, and other threads that need to execute the lock code block can only wait. When one thread completes execution, the object's lock is released, and other threads waiting for the object can enter the code block and repeat the above process.
The implementation of using synchronized is relatively simple. Just add the keyword synchronized before the method or code block that needs to be synchronized. For example:
public synchronized void method1(){ //... }
Or use a code block:
public void method1(){ synchronized(this) { //... } }
Another form of synchronized is a class lock. When using class locks, the monitor is a Class object of the same class. For example:
public class MyClass { public static synchronized void method1(){ //... } }
In the above code, all instances of MyClass share the same Class object of the MyClass class.
2. Use ReentrantLock to achieve synchronization and mutual exclusion
Different from the synchronized keyword, ReentrantLock is a class rather than a keyword. ReentrantLock has similar functions to synchronized, but provides more powerful control of synchronization and mutual exclusion mechanisms. In contrast, the use of ReentrantLock is more flexible, for example, it can achieve fair lock acquisition, count the number of lock acquisitions by a thread, etc.
The usage of ReentrantLock is as follows:
ReentrantLock lock = new ReentrantLock(); lock.lock(); try{ //... } finally { lock.unlock(); }
ReentrantLock is reentrant, that is, the same thread can obtain the lock multiple times.
3. Use CountDownLatch to achieve synchronization
CountDownLatch is a tool class for thread synchronization, which allows one or more threads to wait for another or multiple threads to complete execution. CountDownLatch provides a countdown counter. The thread that needs to wait needs to call the Countdown() method to decrement the counter by 1. When the counter is 0, the waiting thread can continue to execute.
Using CountDownLatch allows one thread to wait for another thread to start before continuing. For example:
//初始化计数器为1 CountDownLatch latch = new CountDownLatch(1); //线程1 new Thread(new Runnable() { public void run(){ //其他操作 latch.countDown(); } }).start(); //线程2等待线程1启动完毕 latch.await(); //线程2继续执行
4. Use Semaphore to achieve synchronization and mutual exclusion
Semaphore is another Java concurrency tool class that allows you to control the number of threads that concurrently access specific resources. Semaphore maintains a set of licenses. When a thread requests a license, it can receive the license and continue execution. When the license is used up, you need to wait for other threads to return the license before continuing.
Use Semaphore to limit the number of threads that access a resource at the same time, for example:
//初始化Semaphore,设定同时允许2个线程访问 Semaphore semaphore = new Semaphore(2); //其他线程请求许可证 semaphore.acquire(); //许可证使用完毕后释放 semaphore.release();
Summary:
The above are several ways to achieve thread synchronization and mutual exclusion in the Java language method, of which synchronized is the most basic implementation method, ReentrantLock provides a more powerful control mechanism, and CountDownLatch and Semaphore can well control the collaboration and concurrency between threads. In actual programming, reasonable selection and flexible use of these tools can effectively avoid thread synchronization and mutual exclusion problems in Java programs.
The above is the detailed content of Implementation method of thread synchronization and mutual exclusion in Java language. For more information, please follow other related articles on the PHP Chinese website!