JAVA多线程初识(6)---Lock解析
程序员文章站
2022-05-04 20:13:18
...
在多线程初识中我们知道Lock实际上是一个接口
Lock是一个接口,其由三个具体的实现:ReentrantLock、ReetrantReadWriteLock.ReadLock 和 ReetrantReadWriteLock.WriteLock,即重入锁、读锁和写锁。
我们来看一下Lock接口的源码:
public interface Lock {
//无条件获取锁
void lock();
//获取可响应中断的锁
//在获取锁的时候可响应中断,中断的时候会抛出中断异常
void lockInterruptibly() throws InterruptedException;
//轮询锁。如果不能获得锁,则采用轮询的方式不断尝试获得锁
boolean tryLock();
//定时锁。如果不能获得锁,则每隔unit的时间就会尝试重新获取锁
boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
//释放获得锁
void unlock();
//获取绑定的Lock实例的条件变量。在等待某个条件变量满足的之
//前,lock实例必须被当前线程持有。调用Condition的await方法
//会自动释放当前线程持有的锁
Condition newCondition();
lock锁的用法:
class Class {
private final ReentrantLock lock = new ReentrantLock();
// ...
public void fun() {
lock.lock(); // block until condition holds
try {
// ... method body
} finally {
lock.unlock()
}
}
}
lock的使用一般都是在try之前调用lock方法,在finally代码块释放锁
使用一个lock锁代码;
package Lock;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class MyService{
private Lock lock = new ReentrantLock(); //用ReentrabtLock接收
public void testMethod() {
lock.lock(); //获取锁
for(int i = 0; i < 5; i++) {
try {
MyThread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("线程名字" + Thread.currentThread().getName() + (" " + (i + 1)));
}
lock.unlock(); //解锁
}
}
class MyThread extends Thread{
private MyService service;
public MyThread(MyService service) {
super();
this.service = service;
}
public void run() {
service.testMethod();
}
}
public class Lock1 {
public static void main(String[] args) {
MyService service = new MyService();
MyThread a1 = new MyThread(service);
MyThread a2 = new MyThread(service);
MyThread a3 = new MyThread(service);
MyThread a4 = new MyThread(service);
MyThread a5 = new MyThread(service);
a1.run();
a2.run();
a3.run();
a4.run();
a5.run();
}
}
从运行的结果来看,当前线程打印之后将锁进行释放,其他线程才可以打印。线程打印的数据是分组打印,当前的线程已经持有锁。
生产者消费者交替打印代码实现:
package Lock;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
class MyService{
private ReentrantLock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
private boolean hasValue = false;
public void set() {
try {
lock.lock();
System.out.println(System.currentTimeMillis());
while (hasValue == true) {
condition.await();
}
System.out.println("打印***");
hasValue = true;
condition.signal();
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void get() {
try {
lock.lock();
while (hasValue == false) {
condition.await();
System.out.println("打印###");
hasValue = false;
condition.signal();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
class MyThreadA extends Thread{
private MyService myService;
public MyThreadA(MyService myService) {
super();
this.myService = myService;
}
public void run() {
for (int i = 0; i < Integer.MAX_VALUE; i++) {
myService.set();
}
}
}
class MyThreadB extends Thread{
private MyService myService;
public MyThreadB(MyService myService) {
super();
this.myService = myService;
}
public void run() {
for (int i = 0; i < Integer.MAX_VALUE; i++) {
myService.get();
}
}
}
public class custom {
public static void main(String[] args) {
MyService myService = new MyService();
MyThreadA a = new MyThreadA(myService);
a.start();
MyThreadB b = new MyThreadB(myService);
b.start();
}
}
上一篇: 线程的生命周期与状态切换
下一篇: Java锁Lock源码分析(一)