非重入锁
计数
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
public class MyLockUnenter implements Lock{
private boolean isLock = false;
@Override
public synchronized void lock() {
// TODO Auto-generated method stub
while (isLock) {
try {
wait();
} catch (Exception e) {
// TODO: handle exception
}
}
isLock = true;
}
@Override
public synchronized void unlock() {
// TODO Auto-generated method stub
isLock = false;
notify();
}
@Override
public void lockInterruptibly() throws InterruptedException {
// TODO Auto-generated method stub
}
@Override
public boolean tryLock() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean tryLock(long time, TimeUnit unit)
throws InterruptedException {
// TODO Auto-generated method stub
return false;
}
@Override
public Condition newCondition() {
// TODO Auto-generated method stub
return null;
}
}
public class TestUnenter {
Lock lock = new MyLockUnenter();
private int value;
private int getnum(){
lock.lock();
int a = value++;
lock.unlock();
return a;
}
public static void main(String[] args) {
TestUnenter testUnenter = new TestUnenter();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
System.out.println(Thread.currentThread().getName()+" "+testUnenter.getnum());
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
System.out.println(Thread.currentThread().getName()+" "+testUnenter.getnum());
}
}
}).start();
}
}
线程无限期等待示例
public class TestUnenter {
Lock lock = new MyLockUnenter();
public void a(){
lock.lock();
System.err.println("a");
b();
lock.unlock();
}
public void b(){
lock.lock();
System.err.println("b");
// b();
lock.unlock();
}
public static void main(String[] args) {
TestUnenter testUnenter = new TestUnenter();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
TestUnenter d = new TestUnenter();
d.a();
}
}).start();
}
}
重入锁,类似于sychronized,不会有死锁情况
public class MyLock implements Lock{
private boolean isLock = false;
private Thread lockby = null;//被锁住的线程
private int locknum ;//锁重入的次数
@Override
public synchronized void lock() {
// TODO Auto-generated method stub
// 先判断是否有线程已经获得锁,判断当前线程是否为锁住的线程
Thread current = Thread.currentThread();
while (isLock&¤t!=lockby) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
lockby = current;
isLock = true;
locknum++;
}
@Override
public synchronized void unlock() {
// TODO Auto-generated method stub
// 判断当前线程是否为锁住的线程
if (lockby == Thread.currentThread()) {
// 重入次数减去1
locknum--;
// 当最后一个锁被释放
if (locknum == 0) {
isLock = false;
notify();
}
}
}
@Override
public void lockInterruptibly() throws InterruptedException {
// TODO Auto-generated method stub
}
@Override
public boolean tryLock() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean tryLock(long time, TimeUnit unit)
throws InterruptedException {
// TODO Auto-generated method stub
return false;
}
@Override
public Condition newCondition() {
// TODO Auto-generated method stub
return null;
}
}
网友评论