forked from Pamgo/demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/main/java/com/example/concurrency/st/interrupt/IntLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.example.concurrency.st.interrupt; | ||
|
||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* 改代码会产生死循环,但是得益于锁的响应中断,解决了死循环这个问题 | ||
* @author OKali | ||
* | ||
*/ | ||
public class IntLock implements Runnable{ | ||
|
||
public static ReentrantLock lock1 = new ReentrantLock(); | ||
public static ReentrantLock lock2 = new ReentrantLock(); | ||
|
||
int lock; | ||
|
||
/** | ||
* 控制加锁顺序,方便构造死循环 | ||
* @param lock | ||
*/ | ||
public IntLock(int lock) { | ||
this.lock = lock; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
if (lock == 1) { | ||
lock1.lockInterruptibly(); | ||
Thread.sleep(500); | ||
lock2.lockInterruptibly(); | ||
} else { | ||
lock2.lockInterruptibly(); | ||
Thread.sleep(500); | ||
lock1.lockInterruptibly(); | ||
} | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (lock1.isHeldByCurrentThread()) { | ||
lock1.unlock(); | ||
} | ||
if (lock2.isHeldByCurrentThread()) { | ||
lock2.unlock(); | ||
} | ||
System.out.println(Thread.currentThread().getId() + "线程退出!"); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
IntLock intLock = new IntLock(1); | ||
Thread t1 = new Thread(intLock); | ||
Thread t2 = new Thread(intLock); | ||
|
||
t1.start();t2.start(); | ||
Thread.sleep(1000); | ||
t1.interrupt(); | ||
} | ||
|
||
|
||
} |