Lock是多线程访问共享资源的工具,通常情况下,一次只能有一个线程获得锁,即互斥锁。不过也有一些所允许对共享资源的并发访问。
ReentrantLock获取锁定与三种方式:
a) lock(), 如果获取了锁立即返回,如果别的线程持有锁,当前线程则一直处于休眠状态,直到获取锁
b) tryLock(), 如果获取了锁立即返回true,如果别的线程正持有锁,立即返回false;
c)tryLock(long timeout,TimeUnit unit), 如果获取了锁定立即返回true,如果别的线程正持有锁,会等待参数给定的时间,在等待的过程中,如果获取了锁定,就返回true,如果等待超时,返回false;
d) lockInterruptibly():如果获取了锁定立即返回,如果没有获取锁定,当前线程处于休眠状态,直到或者锁定,或者当前线程被别的线程中断
ReentrantLock和Synchronized区别:
1 synchronized是在JVM层面上实现的,不但可以通过一些监控工具监控synchronized的锁定,而且在代码执行时出现异常,JVM会自动释放锁定,但是使用Lock则不行,lock是通过代码实现的,要保证锁定一定会被释放,就必须将unLock()放到finally{}中
2 在资源竞争不是很激烈的情况下,Synchronized的性能要优于ReetrantLock,但是在资源竞争很激烈的情况下,Synchronized的性能会下降几十倍,但是ReetrantLock的性能能维持常态;
3 Synchronized非中断 Lock中断锁,非中断锁,定时锁
import java.util.concurrent.locks.*;
import java.util.*;
import java.util.concurrent.*;
class Task
{
private final Lock lock=new ReentrantLock();
private final Condition putCondition=lock.newCondition();
private final Condition takeCondition=lock.newCondition();
private LinkedList<Integer> list=new LinkedList<Integer>();
private static int num=0;
public Task(){}
public void put(int a)
{
lock.lock();
try
{
while(num==10)
{
this.putCondition.await();
}
num++;
list.add(a);
System.out.println("after "+Thread.currentThread().getName()+" put="+a+" current size="+num);
this.takeCondition.signal();
}
catch (InterruptedException e)
{
}
finally
{
this.lock.unlock();
}
}
public void take()
{
lock.lock();
try
{
while(num==0)
{
this.takeCondition.await();
}
num--;
int a=list.remove(0);
System.out.println("after "+Thread.currentThread().getName()+" get="+a+" current size="+num);
this.putCondition.signal();
}
catch (InterruptedException e)
{
}
finally
{
this.lock.unlock();
}
}
}
class PutThread implements Runnable
{
private Task task;
private final CyclicBarrier cb;
public PutThread(Task task,CyclicBarrier cb)
{
this.task=task;
this.cb=cb;
}
public void run()
{
this.task.put(1);
try
{
cb.await();
}
catch (Exception e)
{
}
}
}
class TakeThread implements Runnable
{
private Task task;
private final CyclicBarrier cb;
public TakeThread(Task task,CyclicBarrier cb)
{
this.task=task;
this.cb=cb;
}
public void run()
{
this.task.take();
try
{
cb.await();
}
catch (Exception e)
{
}
}
}
public class TestLock
{
public static void main(String [] args)
{
long start=(new Date()).getTime();
Task t=new Task();
final long begin=(new Date()).getTime();
CyclicBarrier cb =new CyclicBarrier(4000,new Runnable(){
public void run()
{
long end=(new Date()).getTime();
System.out.println(end-begin);
}
});
for(int i=0;i<2000;i++)
{
new Thread(new PutThread(t,cb)).start();
}
for(int j=0;j<2000;j++)
{
new Thread(new TakeThread(t,cb)).start();
}
}
}