本文主要是介绍【Java并发编程十一】同步控制三,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
LockSupport 线程阻塞工具
LockSupport的unpark() 方法可以先执行。
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;
public class myTest implements Runnable{public static void main(String[] args) throws InterruptedException {Thread thread = new Thread(new myTest());thread.start();Thread.sleep(3000);// 继续执行子线程LockSupport.unpark(thread);}@Overridepublic void run() {System.out.println(Thread.currentThread().getId()+"已被阻塞");// 线程被阻塞LockSupport.park();System.out.println("继续执行");}
}
ReadWriteLock 读写锁
将读写操作分离,从而提高程序执行的效率。这个可以使多个线程可以同时读取数据。
ReentrantReadWriteLock.readLock() 读锁
当这个方法开启的时候,在休眠一秒后加了锁,按道理来说,18个线程至少要等18秒才能运行完成,但是对于锁类型是读写锁的线程而言,可以被多个线程重复进入,重入锁和读写锁的区别在于,重入锁是对于本线程的行为可以被重复进入而不加锁,读写锁则是对于多个不进行修改操作的线程,都可以进入而不加锁。
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class myTest{// 可重入锁private static Lock lock = new ReentrantLock();// 可重入的读写锁private static ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();// 读锁private static Lock read = reentrantReadWriteLock.readLock();private int value = 0;public static void main(String[] args) {final myTest test = new myTest();Runnable readrunnable = new Runnable() {@Overridepublic void run() {test.handleRead(read);}};for (int i = 0; i < 18; i++) {new Thread(readrunnable).start();}}public Object handleRead(Lock lock) {try {lock.lock();Thread.sleep(1000);System.out.println("what");return value;} catch (InterruptedException e) {throw new RuntimeException(e);}// 该块代码块无论如何都会被执行finally {lock.unlock();return value;}}
}
ReentrantReadWriteLock.writeLock() 写锁
写锁是一个排它锁,只能被一个线程所获取,这个锁的主要方法,也来自于AQS同步器。
package myTest;import jdk.jshell.EvalException;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class myTest{// 可重入的读写锁private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();private static int value = 0;// 读操作public static void readFile(Thread thread) {Lock read = lock.readLock();read.lock();if(!lock.isWriteLocked()) {System.out.println(thread.getName()+": 当前为读锁");}try {for (int i = 0; i < 5; i++) {try {Thread.sleep(20);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(thread.getName()+"正在读取读取");}System.out.println(thread.getName()+": 读取完毕" + value);//setValue();} finally {System.out.println(thread.getName()+": 释放读锁");read.unlock();}}public static void setValue() {value ++;}// 写操作public static void writeFile(Thread thread) {Lock write = lock.writeLock();if(lock.isWriteLocked()) {System.out.println(thread.getName()+"当前为写锁");}try {for (int i = 0; i < 5; i++) {try {Thread.sleep(20);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(thread.getName()+": 正在写");}value ++;System.out.println(thread.getName()+": 写完毕");} finally {System.out.println(thread.getName()+ ": 释放写锁");write.unlock();}}public static void main(String[] args) {ExecutorService service = Executors.newCachedThreadPool();service.execute(new Runnable() {@Overridepublic void run() {readFile(Thread.currentThread());}});service.execute(new Runnable() {@Overridepublic void run() {writeFile(Thread.currentThread());}});service.execute(new Runnable() {@Overridepublic void run() {readFile(Thread.currentThread());}});service.execute(new Runnable() {@Overridepublic void run() {readFile(Thread.currentThread());}});service.execute(new Runnable() {@Overridepublic void run() {writeFile(Thread.currentThread());}});service.execute(new Runnable() {@Overridepublic void run() {readFile(Thread.currentThread());}});}}
锁降级
写锁可以降级为读锁,读锁不能降级为写锁。
import jdk.jshell.EvalException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class myTest{// 可重入的读写锁private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();static Lock readLock = lock.readLock();static Lock writeLock = lock.writeLock();public static void main(String[] args) {// 1. 获取写锁writeLock.lock();System.out.println("获取写锁");// 2. 获取读锁readLock.lock();System.out.println("获取读锁");// 3. 释放写锁writeLock.unlock();// 4. 释放读锁readLock.unlock();}
}
如下面代码所示。但是在持有读锁时,在有读锁,就会报错。
import jdk.jshell.EvalException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class myTest{// 可重入的读写锁private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();static Lock readLock = lock.readLock();static Lock writeLock = lock.writeLock();public static void main(String[] args) {// 2. 获取读锁readLock.lock();System.out.println("获取读锁"); // 1. 获取写锁writeLock.lock();System.out.println("获取写锁");// 3. 释放写锁writeLock.unlock();// 4. 释放读锁readLock.unlock();}
}
这篇关于【Java并发编程十一】同步控制三的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!