When we do not use polling locks, this problem may occur:
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class DeadLockByReentrantLock { public static void main(String[] args) { Lock lockA = new ReentrantLock(); // 创建锁 A Lock lockB = new ReentrantLock(); // 创建锁 B // 创建线程 1 Thread t1 = new Thread(new Runnable() { @Override public void run() { lockA.lock(); // 加锁 System.out.println("线程 1:获取到锁 A!"); try { Thread.sleep(1000); System.out.println("线程 1:等待获取 B..."); lockB.lock(); // 加锁 try { System.out.println("线程 1:获取到锁 B!"); } finally { lockA.unlock(); // 释放锁 } } catch (InterruptedException e) { e.printStackTrace(); } finally { lockA.unlock(); // 释放锁 } } }); t1.start(); // 运行线程 // 创建线程 2 Thread t2 = new Thread(new Runnable() { @Override public void run() { lockB.lock(); // 加锁 System.out.println("线程 2:获取到锁 B!"); try { Thread.sleep(1000); System.out.println("线程 2:等待获取 A..."); lockA.lock(); // 加锁 try { System.out.println("线程 2:获取到锁 A!"); } finally { lockA.unlock(); // 释放锁 } } catch (InterruptedException e) { e.printStackTrace(); } finally { lockB.unlock(); // 释放锁 } } }); t2.start(); // 运行线程 } }
The execution results of the above code are as follows:
As can be seen from the above results, at this time, the program has threads waiting for each other and trying to obtain each other's (lock) resources. This is a typical situation. Deadlock problem.
When a deadlock problem occurs, we can use polling lock to solve it. Its implementation idea is to obtain multiple Lock, if any lock acquisition fails during the process, a rollback operation is performed, all locks owned by the current thread are released, and wait for the next re-execution. This can prevent multiple threads from owning and occupying the lock resources at the same time, thus directly solving the problem. For the problem of deadlock, the simple version of polling lock is implemented as follows:
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class SolveDeadLockExample2 { public static void main(String[] args) { Lock lockA = new ReentrantLock(); // 创建锁 A Lock lockB = new ReentrantLock(); // 创建锁 B // 创建线程 1(使用轮询锁) Thread t1 = new Thread(new Runnable() { @Override public void run() { // 调用轮询锁 pollingLock(lockA, lockB); } }); t1.start(); // 运行线程 // 创建线程 2 Thread t2 = new Thread(new Runnable() { @Override public void run() { lockB.lock(); // 加锁 System.out.println("线程 2:获取到锁 B!"); try { Thread.sleep(1000); System.out.println("线程 2:等待获取 A..."); lockA.lock(); // 加锁 try { System.out.println("线程 2:获取到锁 A!"); } finally { lockA.unlock(); // 释放锁 } } catch (InterruptedException e) { e.printStackTrace(); } finally { lockB.unlock(); // 释放锁 } } }); t2.start(); // 运行线程 } /** * 轮询锁 */ private static void pollingLock(Lock lockA, Lock lockB) { // 轮询锁 while (true) { if (lockA.tryLock()) { // 尝试获取锁 System.out.println("线程 1:获取到锁 A!"); try { Thread.sleep(1000); System.out.println("线程 1:等待获取 B..."); if (lockB.tryLock()) { // 尝试获取锁 try { System.out.println("线程 1:获取到锁 B!"); } finally { lockB.unlock(); // 释放锁 System.out.println("线程 1:释放锁 B."); break; } } } catch (InterruptedException e) { e.printStackTrace(); } finally { lockA.unlock(); // 释放锁 System.out.println("线程 1:释放锁 A."); } } // 等待一秒再继续执行 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
The execution result of the above code is as follows:
It can be seen from the above results that when we use polling locks in the program, deadlock problems will not occur, but the above polling locks are not perfect. Let’s take a look at this What kind of problems will there be with polling locks?
For the above simple version of the polling lock, if a thread continues to occupy the lock resource or occupies the lock resource for a long time, it will cause the polling lock to enter In an infinite loop state, it will try to keep acquiring lock resources, which will cause new problems and unnecessary performance overhead. Specific examples are as follows.
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class SolveDeadLockExample { public static void main(String[] args) { Lock lockA = new ReentrantLock(); // 创建锁 A Lock lockB = new ReentrantLock(); // 创建锁 B // 创建线程 1(使用轮询锁) Thread t1 = new Thread(new Runnable() { @Override public void run() { // 调用轮询锁 pollingLock(lockA, lockB); } }); t1.start(); // 运行线程 // 创建线程 2 Thread t2 = new Thread(new Runnable() { @Override public void run() { lockB.lock(); // 加锁 System.out.println("线程 2:获取到锁 B!"); try { Thread.sleep(1000); System.out.println("线程 2:等待获取 A..."); lockA.lock(); // 加锁 try { System.out.println("线程 2:获取到锁 A!"); } finally { lockA.unlock(); // 释放锁 } } catch (InterruptedException e) { e.printStackTrace(); } finally { // 如果此处代码未执行,线程 2 一直未释放锁资源 // lockB.unlock(); } } }); t2.start(); // 运行线程 } /** * 轮询锁 */ public static void pollingLock(Lock lockA, Lock lockB) { while (true) { if (lockA.tryLock()) { // 尝试获取锁 System.out.println("线程 1:获取到锁 A!"); try { Thread.sleep(1000); System.out.println("线程 1:等待获取 B..."); if (lockB.tryLock()) { // 尝试获取锁 try { System.out.println("线程 1:获取到锁 B!"); } finally { lockB.unlock(); // 释放锁 System.out.println("线程 1:释放锁 B."); break; } } } catch (InterruptedException e) { e.printStackTrace(); } finally { lockA.unlock(); // 释放锁 System.out.println("线程 1:释放锁 A."); } } // 等待一秒再继续执行 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
The execution results of the above code are as follows:
As can be seen from the above results, the thread 1 The polling lock enters an infinite loop state.
In view of the above infinite loop situation, we can improve the following two ideas:
Add the maximum number of times limit: If the lock has not been obtained after n attempts to obtain the lock, it is considered that the lock acquisition failed, and the polling is terminated after the failure strategy is executed (the failure strategy can be logging or other operations) ;
Add the maximum duration limit: If the lock has not been obtained after n seconds of trying to obtain the lock, it is considered that the lock acquisition failed, and after the failure strategy is executed Terminate polling.
Any one of the above strategies can solve the problem of infinite loop. For the sake of implementation cost, we can use the maximum number of polls to improve the polling lock,
The specific implementation code is as follows:
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class SolveDeadLockExample { public static void main(String[] args) { Lock lockA = new ReentrantLock(); // 创建锁 A Lock lockB = new ReentrantLock(); // 创建锁 B // 创建线程 1(使用轮询锁) Thread t1 = new Thread(new Runnable() { @Override public void run() { // 调用轮询锁 pollingLock(lockA, lockB, 3); } }); t1.start(); // 运行线程 // 创建线程 2 Thread t2 = new Thread(new Runnable() { @Override public void run() { lockB.lock(); // 加锁 System.out.println("线程 2:获取到锁 B!"); try { Thread.sleep(1000); System.out.println("线程 2:等待获取 A..."); lockA.lock(); // 加锁 try { System.out.println("线程 2:获取到锁 A!"); } finally { lockA.unlock(); // 释放锁 } } catch (InterruptedException e) { e.printStackTrace(); } finally { // 线程 2 忘记释放锁资源 // lockB.unlock(); // 释放锁 } } }); t2.start(); // 运行线程 } /** * 轮询锁 * * maxCount:最大轮询次数 */ public static void pollingLock(Lock lockA, Lock lockB, int maxCount) { // 轮询次数计数器 int count = 0; while (true) { if (lockA.tryLock()) { // 尝试获取锁 System.out.println("线程 1:获取到锁 A!"); try { Thread.sleep(1000); System.out.println("线程 1:等待获取 B..."); if (lockB.tryLock()) { // 尝试获取锁 try { System.out.println("线程 1:获取到锁 B!"); } finally { lockB.unlock(); // 释放锁 System.out.println("线程 1:释放锁 B."); break; } } } catch (InterruptedException e) { e.printStackTrace(); } finally { lockA.unlock(); // 释放锁 System.out.println("线程 1:释放锁 A."); } } // 判断是否已经超过最大次数限制 if (count++ > maxCount) { // 终止循环 System.out.println("轮询锁获取失败,记录日志或执行其他失败策略"); return; } // 等待一秒再继续尝试获取锁 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
The execution results of the above code are as follows:
It can be seen from the above results that after we improve it, the polling lock will not have the problem of infinite loop. It will terminate execution after trying a certain number of times.
The polling waiting time of our above polling lock is a fixed time, as shown in the following code:
// 等待 1s 再尝试获取(轮询)锁 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
This will cause the problem of thread starvation under special circumstances, that is, the problem of polling locks never being able to obtain the lock, such as the following example.
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class SolveDeadLockExample { public static void main(String[] args) { Lock lockA = new ReentrantLock(); // 创建锁 A Lock lockB = new ReentrantLock(); // 创建锁 B // 创建线程 1(使用轮询锁) Thread t1 = new Thread(new Runnable() { @Override public void run() { // 调用轮询锁 pollingLock(lockA, lockB, 3); } }); t1.start(); // 运行线程 // 创建线程 2 Thread t2 = new Thread(new Runnable() { @Override public void run() { while (true) { lockB.lock(); // 加锁 System.out.println("线程 2:获取到锁 B!"); try { System.out.println("线程 2:等待获取 A..."); lockA.lock(); // 加锁 try { System.out.println("线程 2:获取到锁 A!"); } finally { lockA.unlock(); // 释放锁 } } finally { lockB.unlock(); // 释放锁 } // 等待一秒之后继续执行 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); t2.start(); // 运行线程 } /** * 轮询锁 */ public static void pollingLock(Lock lockA, Lock lockB, int maxCount) { // 循环次数计数器 int count = 0; while (true) { if (lockA.tryLock()) { // 尝试获取锁 System.out.println("线程 1:获取到锁 A!"); try { Thread.sleep(100); // 等待 0.1s(获取锁需要的时间) System.out.println("线程 1:等待获取 B..."); if (lockB.tryLock()) { // 尝试获取锁 try { System.out.println("线程 1:获取到锁 B!"); } finally { lockB.unlock(); // 释放锁 System.out.println("线程 1:释放锁 B."); break; } } } catch (InterruptedException e) { e.printStackTrace(); } finally { lockA.unlock(); // 释放锁 System.out.println("线程 1:释放锁 A."); } } // 判断是否已经超过最大次数限制 if (count++ > maxCount) { // 终止循环 System.out.println("轮询锁获取失败,记录日志或执行其他失败策略"); return; } // 等待一秒再继续尝试获取锁 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
The execution results of the above code are as follows:
As can be seen from the above results, the thread 1 (polling lock) has not successfully acquired the lock. The reason for this result is: Thread 1's waiting time for each poll is fixed 1s, and Thread 2 also has the same frequency, acquiring the lock every 1s, so This will cause thread 2 to always successfully acquire the lock first, while thread 1 will always be in a "starving" situation. The execution process is as shown in the figure below:
Next, we can improve the fixed waiting time of the polling lock to a fixed time random time method, so that we can avoid the problem of acquiring the lock. The frequency is consistent, which causes the problem of "starvation" of the polling lock. The specific implementation code is as follows:
import java.util.Random; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class SolveDeadLockExample { private static Random rdm = new Random(); public static void main(String[] args) { Lock lockA = new ReentrantLock(); // 创建锁 A Lock lockB = new ReentrantLock(); // 创建锁 B // 创建线程 1(使用轮询锁) Thread t1 = new Thread(new Runnable() { @Override public void run() { // 调用轮询锁 pollingLock(lockA, lockB, 3); } }); t1.start(); // 运行线程 // 创建线程 2 Thread t2 = new Thread(new Runnable() { @Override public void run() { while (true) { lockB.lock(); // 加锁 System.out.println("线程 2:获取到锁 B!"); try { System.out.println("线程 2:等待获取 A..."); lockA.lock(); // 加锁 try { System.out.println("线程 2:获取到锁 A!"); } finally { lockA.unlock(); // 释放锁 } } finally { lockB.unlock(); // 释放锁 } // 等待一秒之后继续执行 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); t2.start(); // 运行线程 } /** * 轮询锁 */ public static void pollingLock(Lock lockA, Lock lockB, int maxCount) { // 循环次数计数器 int count = 0; while (true) { if (lockA.tryLock()) { // 尝试获取锁 System.out.println("线程 1:获取到锁 A!"); try { Thread.sleep(100); // 等待 0.1s(获取锁需要的时间) System.out.println("线程 1:等待获取 B..."); if (lockB.tryLock()) { // 尝试获取锁 try { System.out.println("线程 1:获取到锁 B!"); } finally { lockB.unlock(); // 释放锁 System.out.println("线程 1:释放锁 B."); break; } } } catch (InterruptedException e) { e.printStackTrace(); } finally { lockA.unlock(); // 释放锁 System.out.println("线程 1:释放锁 A."); } } // 判断是否已经超过最大次数限制 if (count++ > maxCount) { // 终止循环 System.out.println("轮询锁获取失败,记录日志或执行其他失败策略"); return; } // 等待一定时间(固定时间 + 随机时间)之后再继续尝试获取锁 try { Thread.sleep(300 + rdm.nextInt(8) * 100); // 固定时间 + 随机时间 } catch (InterruptedException e) { e.printStackTrace(); } } } }
The execution results of the above code are as follows:
It can be seen from the above results that after thread 1 (polling lock) adds a random waiting time, the problem of thread starvation will not occur.The above is the detailed content of How to solve problems when using polling locks in Java?. For more information, please follow other related articles on the PHP Chinese website!