即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

Lock运用

编程语言 chenmeng930601 13℃ 0评论

1ReentrantLock实现同步

package service;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyService {
    private Lock lock=new ReentrantLock();
    public void testMethod(){
        lock.lock();
        for(int i=0;i<5;i++){
            System.out.println("ThreadName="+Thread.currentThread().getName()+(" "+(i+1)));
        }
        lock.unlock();
    }

}


package service;

public class MyThread extends Thread{
    private MyService service;
    public MyThread(MyService service){
        super();
        this.service=service;

    }
    @Override
    public void run(){
        service.testMethod();
    }

}

2借助Condition对象实现等待/通知模式


在使用notify()/notifyAll()方法进行通知时,被通知的线程是由JVM随机选择的;使用ReentrantLock结合Condition类可以实现“选择性通知”。synchronized相当于整个Lock对象只有一个Condition对象,所有线程都注册在这一个对象上。


object类中的wait()方法相当于Condition类的await()方法


object.wait(long timeout) condition.await(long time,TimeUnit unit)


object.notify() condition.signal()


object.notifyAll() condition.signalAll()

package service;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class MyService {
    private ReentrantLock lock=new ReentrantLock();
    private Condition conditionA=lock.newCondition();
    private Condition conditionB=lock.newCondition();
    private boolean hasValue=false;
    public void set(){
        try{
            lock.lock();
            while(hasValue==true){
                System.out.println("有可能**连续");
                conditionA.await();
            }
            System.out.println("打印*");
            hasValue=true;
            conditionB.signal();
        }catch(InterruptedException e){
            e.printStackTrace();
        }finally{
            lock.unlock();
        }
    }

    public void get(){
        try{
            lock.lock();
            while(hasValue==false){
                System.out.println("有可能&&连续");
                conditionB.await();
            }
            System.out.println("打印&");
            hasValue=false;
            conditionA.signal();
        }catch(InterruptedException e){
            e.printStackTrace();
        }finally{
            lock.unlock();
        }
    }
}


package service;
public class MyThreadA extends Thread{
    private MyService myService;
    public MyThreadA(MyService myService){
        super();
        this.myService=myService;
    }
    @Override
    public void run(){
        for(int i=0;ipackage service;
public class MyThreadB extends Thread{
    private MyService myService;
    public MyThreadB(MyService myService){
        super();
        this.myService=myService;
    }
    @Override
    public void run(){
        for(int i=0;ipackage service;
public class Run {
    public static void main(String[] args){
        MyService service=new MyService();
        MyThreadA[] threadA=new MyThreadA[10];
        MyThreadB[] threadB=new MyThreadB[10];
        for(int i=0;i<10;i++){
            threadA[i]=new MyThreadA(service);
            threadB[i]=new MyThreadB(service);
            threadA[i].start();
            threadB[i].start();

        }   
    }
}

3公平锁和非公平锁


公平锁表示线程获取锁的顺序是按线程加锁的顺序执行的,非公平锁是一种抢占机制,是随机获得锁的,可能某个线程一直拿不到锁,结果也就是不公平的。ReentrantLock默认情况下是非公平锁。


ReentrantLock lock=new ReentrantLock(true);//公平锁

4ReentrantReadWriteLock读写锁


读写锁有两个锁,一个是读操作相关的锁,也称为共享锁;另一个是写操作相关的锁,也叫排它锁。多个读锁之间不互斥,读锁与写锁互斥,写锁与写锁互斥

package service;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Service {
    private ReentrantReadWriteLock lock=new ReentrantReadWriteLock();
    public void read(){
        try{
            try{
                lock.readLock().lock();
                System.out.println("获得读锁");
                Thread.sleep(10000);
            }finally{
                lock.readLock().unlock();
            }
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }

    public void write(){
        try{
            try{
                lock.writeLock().lock();
                System.out.println("获得写锁");
                Thread.sleep(10000);
            }finally{
                lock.writeLock().unlock();
            }
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}

转载请注明:CodingBlog » Lock运用

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情