Home  >  Article  >  Java  >  Detailed explanation of java concurrent programming

Detailed explanation of java concurrent programming

尚
forward
2019-12-25 17:12:112608browse

Detailed explanation of java concurrent programming

1. Defects of synchronized

synchronized is a keyword in java, that is to say It is a built-in feature of the Java language. So why does Lock appear?

If a code block is modified by synchronized, when a thread acquires the corresponding lock and executes the code block, other threads can only wait forever, waiting for the thread that acquired the lock to release the lock, and get it here There are only two situations when the lock's thread releases the lock:

1) The thread that acquired the lock finishes executing the code block, and then the thread releases its possession of the lock;

2) An exception occurs during thread execution , at this time the JVM will let the thread automatically release the lock.

Recommendation: Basic Java Tutorial

Then if the thread that acquires the lock is blocked due to waiting for IO or other reasons (such as calling the sleep method), but it does not After releasing the lock, other threads can only wait dryly. Just imagine how this affects the efficiency of program execution.

Therefore, there is a need for a mechanism that prevents the waiting thread from waiting indefinitely (such as only waiting for a certain time or being able to respond to interrupts), which can be done through Lock.

Another example: when multiple threads read and write files, read operations and write operations will conflict, write operations will conflict with write operations, but read operations and read operations will not occur. conflict phenomenon.

But using the synchronized keyword to achieve synchronization will lead to a problem:

If multiple threads are only performing read operations, so when one thread is performing a read operation, other threads Can only wait and cannot perform read operations.

Therefore, a mechanism is needed to prevent conflicts between threads when multiple threads are only performing read operations. This can be done through Lock.

In addition, through Lock you can know whether the thread has successfully acquired the lock. This is something synchronized cannot do.

To summarize, Lock provides more functions than synchronized. But pay attention to the following points:

1) Lock is not built into the Java language. synchronized is a keyword in the Java language, so it is a built-in feature. Lock is a class through which synchronous access can be achieved;

2) There is a very big difference between Lock and synchronized. Using synchronized does not require the user to manually release the lock. When the synchronized method or synchronized code block is executed, After that, the system will automatically let the thread release the lock; with Lock, the user must manually release the lock. If the lock is not actively released, a deadlock may occur.

2. Commonly used classes under the java.util.concurrent.locks package

Let’s discuss java.util.concurrent. Commonly used classes and interfaces in the locks package.

1.Lock

The first thing to explain is Lock. By looking at the source code of Lock, we can see that Lock is an interface:

public interface Lock {
    void lock();
    void lockInterruptibly() throws InterruptedException;
    boolean tryLock();
    boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
    void unlock();
    Condition newCondition();
}

Let’s go through them one by one. Describe the use of each method in the Lock interface. lock(), tryLock(), tryLock(long time, TimeUnit unit) and lockInterruptibly() are used to obtain locks. The unLock() method is used to release the lock. The newCondition() method will not be described here for the time being, but will be discussed in a later article on thread collaboration.

Four methods are declared in Lock to obtain the lock, so what are the differences between these four methods?

First of all, the lock() method is the most commonly used method, which is used to obtain locks. If the lock has been acquired by another thread, wait.

As mentioned earlier, if you use Lock, you must actively release the lock, and when an exception occurs, the lock will not be automatically released. Therefore, generally speaking, the use of Lock must be carried out in the try{}catch{} block, and the operation of releasing the lock must be carried out in the finally block to ensure that the lock must be released and prevent the occurrence of deadlock. If Lock is usually used for synchronization, it is used in the following form:

Lock lock = ...;
lock.lock();
try{
    //处理任务
}catch(Exception ex){
     
}finally{
    lock.unlock();   //释放锁
}

tryLock() method has a return value, which means it is used to try to acquire the lock. If the acquisition is successful, it returns true , if the acquisition fails (that is, the lock has been acquired by another thread), false is returned, which means that this method will return immediately no matter what. You won't be waiting there when you can't get the lock.

The tryLock(long time, TimeUnit unit) method is similar to the tryLock() method, but the only difference is that this method will wait for a certain period of time when it cannot get the lock. If it still gets the lock within the time limit, If the lock is not reached, false is returned. Returns true if the lock was obtained initially or during the waiting period.

So, under normal circumstances, when acquiring a lock through tryLock, it is used like this:

Lock lock = ...;
if(lock.tryLock()) {
     try{
         //处理任务
     }catch(Exception ex){
         
     }finally{
         lock.unlock();   //释放锁
     } 
}else {
    //如果不能获取锁,则直接做其他事情
}

The lockInterruptibly() method is special. When acquiring a lock through this method, if the thread is waiting to acquire lock, then this thread can respond to the interrupt, that is, interrupt the waiting state of the thread.

That is to say, when two threads want to acquire a lock through lock.lockInterruptibly() at the same time, if thread A acquires the lock at this time, and thread B is only waiting, then call threadB on thread B The .interrupt() method can interrupt the waiting process of thread B.

由于lockInterruptibly()的声明中抛出了异常,所以lock.lockInterruptibly()必须放在try块中或者在调用lockInterruptibly()的方法外声明抛出InterruptedException。

因此lockInterruptibly()一般的使用形式如下:

public void method() throws InterruptedException {
    lock.lockInterruptibly();
    try {  
     //.....
    }
    finally {
        lock.unlock();
    }  
}

注意,当一个线程获取了锁之后,是不会被interrupt()方法中断的。因为本身在前面的文章中讲过单独调用interrupt()方法不能中断正在运行过程中的线程,只能中断阻塞过程中的线程。

因此当通过lockInterruptibly()方法获取某个锁时,如果不能获取到,只有进行等待的情况下,是可以响应中断的。

而用synchronized修饰的话,当一个线程处于等待某个锁的状态,是无法被中断的,只有一直等待下去。

2.ReentrantLock

ReentrantLock,意思是“可重入锁”,关于可重入锁的概念在下一节讲述。ReentrantLock是唯一实现了Lock接口的类,并且ReentrantLock提供了更多的方法。下面通过一些实例看具体看一下如何使用ReentrantLock。

例子1,lock()的正确使用方法

public class Test {
    private ArrayList<Integer> arrayList = new ArrayList<Integer>();
    public static void main(String[] args)  {
        final Test test = new Test();
         
        new Thread(){
            public void run() {
                test.insert(Thread.currentThread());
            };
        }.start();
         
        new Thread(){
            public void run() {
                test.insert(Thread.currentThread());
            };
        }.start();
    }  
     
    public void insert(Thread thread) {
        Lock lock = new ReentrantLock();    //注意这个地方
        lock.lock();
        try {
            System.out.println(thread.getName()+"得到了锁");
            for(int i=0;i<5;i++) {
                arrayList.add(i);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }finally {
            System.out.println(thread.getName()+"释放了锁");
            lock.unlock();
        }
    }
}

输出结果:

Thread-0得到了锁
Thread-1得到了锁
Thread-0释放了锁
Thread-1释放了锁

在insert方法中的lock变量是局部变量,每个线程执行该方法时都会保存一个副本,那么理所当然每个线程执行到lock.lock()处获取的是不同的锁,所以就不会发生冲突。

知道了原因改起来就比较容易了,只需要将lock声明为类的属性即可。

public class Test {
    private ArrayList<Integer> arrayList = new ArrayList<Integer>();
    private Lock lock = new ReentrantLock();    //注意这个地方
    public static void main(String[] args)  {
        final Test test = new Test();
         
        new Thread(){
            public void run() {
                test.insert(Thread.currentThread());
            };
        }.start();
         
        new Thread(){
            public void run() {
                test.insert(Thread.currentThread());
            };
        }.start();
    }  
     
    public void insert(Thread thread) {
        lock.lock();
        try {
            System.out.println(thread.getName()+"得到了锁");
            for(int i=0;i<5;i++) {
                arrayList.add(i);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }finally {
            System.out.println(thread.getName()+"释放了锁");
            lock.unlock();
        }
    }
}

这样就是正确地使用Lock的方法了。

例子2,tryLock()的使用方法

public class Test {
    private ArrayList<Integer> arrayList = new ArrayList<Integer>();
    private Lock lock = new ReentrantLock();    //注意这个地方
    public static void main(String[] args)  {
        final Test test = new Test();
         
        new Thread(){
            public void run() {
                test.insert(Thread.currentThread());
            };
        }.start();
         
        new Thread(){
            public void run() {
                test.insert(Thread.currentThread());
            };
        }.start();
    }  
     
    public void insert(Thread thread) {
        if(lock.tryLock()) {
            try {
                System.out.println(thread.getName()+"得到了锁");
                for(int i=0;i<5;i++) {
                    arrayList.add(i);
                }
            } catch (Exception e) {
                // TODO: handle exception
            }finally {
                System.out.println(thread.getName()+"释放了锁");
                lock.unlock();
            }
        } else {
            System.out.println(thread.getName()+"获取锁失败");
        }
    }
}

输出结果:

Thread-0得到了锁
Thread-1获取锁失败
Thread-0释放了锁

例子3,lockInterruptibly()响应中断的使用方法:

public class Test {
    private Lock lock = new ReentrantLock();   
    public static void main(String[] args)  {
        Test test = new Test();
        MyThread thread1 = new MyThread(test);
        MyThread thread2 = new MyThread(test);
        thread1.start();
        thread2.start();
         
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread2.interrupt();
    }  
     
    public void insert(Thread thread) throws InterruptedException{
        lock.lockInterruptibly();   //注意,如果需要正确中断等待锁的线程,必须将获取锁放在外面,然后将InterruptedException抛出
        try {  
            System.out.println(thread.getName()+"得到了锁");
            long startTime = System.currentTimeMillis();
            for(    ;     ;) {
                if(System.currentTimeMillis() - startTime >= Integer.MAX_VALUE)
                    break;
                //插入数据
            }
        }
        finally {
            System.out.println(Thread.currentThread().getName()+"执行finally");
            lock.unlock();
            System.out.println(thread.getName()+"释放了锁");
        }  
    }
}
 
class MyThread extends Thread {
    private Test test = null;
    public MyThread(Test test) {
        this.test = test;
    }
    @Override
    public void run() {
         
        try {
            test.insert(Thread.currentThread());
        } catch (InterruptedException e) {
            System.out.println(Thread.currentThread().getName()+"被中断");
        }
    }
}

运行之后,发现thread2能够被正确中断。

3.ReadWriteLock

ReadWriteLock也是一个接口,在它里面只定义了两个方法:

public interface ReadWriteLock {
    /**
     * Returns the lock used for reading.
     *
     * @return the lock used for reading.
     */
    Lock readLock();
 
    /**
     * Returns the lock used for writing.
     *
     * @return the lock used for writing.
     */
    Lock writeLock();
}

一个用来获取读锁,一个用来获取写锁。也就是说将文件的读写操作分开,分成2个锁来分配给线程,从而使得多个线程可以同时进行读操作。下面的ReentrantReadWriteLock实现了ReadWriteLock接口。

4.ReentrantReadWriteLock

ReentrantReadWriteLock里面提供了很多丰富的方法,不过最主要的有两个方法:readLock()和writeLock()用来获取读锁和写锁。

下面通过几个例子来看一下ReentrantReadWriteLock具体用法。

假如有多个线程要同时进行读操作的话,先看一下synchronized达到的效果:

public class Test {
    private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
     
    public static void main(String[] args)  {
        final Test test = new Test();
         
        new Thread(){
            public void run() {
                test.get(Thread.currentThread());
            };
        }.start();
         
        new Thread(){
            public void run() {
                test.get(Thread.currentThread());
            };
        }.start();
         
    }  
     
    public synchronized void get(Thread thread) {
        long start = System.currentTimeMillis();
        while(System.currentTimeMillis() - start <= 1) {
            System.out.println(thread.getName()+"正在进行读操作");
        }
        System.out.println(thread.getName()+"读操作完毕");
    }
}

这段程序的输出结果会是,直到thread1执行完读操作之后,才会打印thread2执行读操作的信息。

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0读操作完毕

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1读操作完毕

而改成用读写锁的话:

public class Test {
    private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
     
    public static void main(String[] args)  {
        final Test test = new Test();
         
        new Thread(){
            public void run() {
                test.get(Thread.currentThread());
            };
        }.start();
         
        new Thread(){
            public void run() {
                test.get(Thread.currentThread());
            };
        }.start();
         
    }  
     
    public void get(Thread thread) {
        rwl.readLock().lock();
        try {
            long start = System.currentTimeMillis();
             
            while(System.currentTimeMillis() - start <= 1) {
                System.out.println(thread.getName()+"正在进行读操作");
            }
            System.out.println(thread.getName()+"读操作完毕");
        } finally {
            rwl.readLock().unlock();
        }
    }
}

此时打印的结果为:

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-0正在进行读操作

Thread-1正在进行读操作

Thread-0读操作完毕

Thread-1读操作完毕

说明thread1和thread2在同时进行读操作。

这样就大大提升了读操作的效率。

不过要注意的是,如果有一个线程已经占用了读锁,则此时其他线程如果要申请写锁,则申请写锁的线程会一直等待释放读锁。

如果有一个线程已经占用了写锁,则此时其他线程如果申请写锁或者读锁,则申请的线程会一直等待释放写锁。

关于ReentrantReadWriteLock类中的其他方法感兴趣的朋友可以自行查阅API文档。

5.Lock和synchronized的选择

总结来说,Lock和synchronized有以下几点不同:

1)Lock是一个接口,而synchronized是Java中的关键字,synchronized是内置的语言实现;

2)synchronized在发生异常时,会自动释放线程占有的锁,因此不会导致死锁现象发生;而Lock在发生异常时,如果没有主动通过unLock()去释放锁,则很可能造成死锁现象,因此使用Lock时需要在finally块中释放锁;

3)Lock可以让等待锁的线程响应中断,而synchronized却不行,使用synchronized时,等待的线程会一直等待下去,不能够响应中断;

4)通过Lock可以知道有没有成功获取锁,而synchronized却无法办到。

5)Lock可以提高多个线程进行读操作的效率。

在性能上来说,如果竞争资源不激烈,两者的性能是差不多的,而当竞争资源非常激烈时(即有大量线程同时竞争),此时Lock的性能要远远优于synchronized。所以说,在具体使用时要根据适当情况选择。

三.锁的相关概念介绍

在前面介绍了Lock的基本使用,这一节来介绍一下与锁相关的几个概念。

1.可重入锁

如果锁具备可重入性,则称作为可重入锁。像synchronized和ReentrantLock都是可重入锁,可重入性在我看来实际上表明了锁的分配机制:基于线程的分配,而不是基于方法调用的分配。举个简单的例子,当一个线程执行到某个synchronized方法时,比如说method1,而在method1中会调用另外一个synchronized方法method2,此时线程不必重新去申请锁,而是可以直接执行方法method2。

看下面这段代码就明白了:

class MyClass {
    public synchronized void method1() {
        method2();
    }
     
    public synchronized void method2() {
         
    }
}

上述代码中的两个方法method1和method2都用synchronized修饰了,假如某一时刻,线程A执行到了method1,此时线程A获取了这个对象的锁,而由于method2也是synchronized方法,假如synchronized不具备可重入性,此时线程A需要重新申请锁。但是这就会造成一个问题,因为线程A已经持有了该对象的锁,而又在申请获取该对象的锁,这样就会线程A一直等待永远不会获取到的锁。

而由于synchronized和Lock都具备可重入性,所以不会发生上述现象。

2.可中断锁

可中断锁:顾名思义,就是可以相应中断的锁。

在Java中,synchronized就不是可中断锁,而Lock是可中断锁。

如果某一线程A正在执行锁中的代码,另一线程B正在等待获取该锁,可能由于等待时间过长,线程B不想等待了,想先处理其他事情,我们可以让它中断自己或者在别的线程中中断它,这种就是可中断锁。

在前面演示lockInterruptibly()的用法时已经体现了Lock的可中断性。

3.公平锁

公平锁即尽量以请求锁的顺序来获取锁。比如同是有多个线程在等待一个锁,当这个锁被释放时,等待时间最久的线程(最先请求的线程)会获得该所,这种就是公平锁。

非公平锁即无法保证锁的获取是按照请求锁的顺序进行的。这样就可能导致某个或者一些线程永远获取不到锁。

在Java中,synchronized就是非公平锁,它无法保证等待的线程获取锁的顺序。

而对于ReentrantLock和ReentrantReadWriteLock,它默认情况下是非公平锁,但是可以设置为公平锁。

看一下这2个类的源代码就清楚了:

Detailed explanation of java concurrent programming

在ReentrantLock中定义了2个静态内部类,一个是NotFairSync,一个是FairSync,分别用来实现非公平锁和公平锁。

我们可以在创建ReentrantLock对象时,通过以下方式来设置锁的公平性:

ReentrantLock lock = new ReentrantLock(true);

如果参数为true表示为公平锁,为fasle为非公平锁。默认情况下,如果使用无参构造器,则是非公平锁。

Detailed explanation of java concurrent programming

另外在ReentrantLock类中定义了很多方法,比如:

isFair()        //判断锁是否是公平锁

isLocked()    //判断锁是否被任何线程获取了

isHeldByCurrentThread()   //判断锁是否被当前线程获取了

hasQueuedThreads()   //判断是否有线程在等待该锁

在ReentrantReadWriteLock中也有类似的方法,同样也可以设置为公平锁和非公平锁。不过要记住,ReentrantReadWriteLock并未实现Lock接口,它实现的是ReadWriteLock接口。

4.读写锁

读写锁将对一个资源(比如文件)的访问分成了2个锁,一个读锁和一个写锁。

正因为有了读写锁,才使得多个线程之间的读操作不会发生冲突。

ReadWriteLock就是读写锁,它是一个接口,ReentrantReadWriteLock实现了这个接口。

可以通过readLock()获取读锁,通过writeLock()获取写锁。

上面已经演示过了读写锁的使用方法,在此不再赘述。

原文地址:http://www.cnblogs.com/dolphin0520/p/3923167.html

The above is the detailed content of Detailed explanation of java concurrent programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete