并发主要有三块知识点:
synchronized / 同步容器 / ThreadPool,executor
Lock/ReentrantLock
Lock lock = new ReentrantLock();
//公平锁 Lock lock = new ReentrantLock(true);
void m1(){
lock.lock();//synchronized(this)
//或 lock.lockInterruptibly(); 加锁,且可以被打断
for (int i =0;i<5;i++) System.out.println("m1 : 第 "+(i+1)+" 个元素");
lock.unlock();
}
Boolean f =lock.tryLock(); //尝试锁定
try {
Boolean f1 =lock.tryLock(5,TimeUnit.SECONDS); //尝试锁定,等待5秒,然后锁定了干嘛,不锁定干嘛
} catch (InterruptedException e) {
e.printStackTrace();
}
可以被打断的锁:t1 锁一直不放,t2不打算让他等了,打断t2
public class MyThreadFour {
//锁
Lock lock = new ReentrantLock();
void m2(){
try {
lock.lock();
for (int i=0;i<1000;i++){
System.out.println(" i : "+i);
TimeUnit.SECONDS.sleep(1);
}
} catch (InterruptedException e) {
System.out.println("已经被打断");
}finally {
if (lock.tryLock())lock.unlock();
}
}
void m1(){
try {
lock.lockInterruptibly();
System.out.println("跑进了m1");
} catch (InterruptedException e) {
System.out.println("m1 被打断");
}
}
public static void main(String[] args) {
MyThreadFour four = new MyThreadFour();
Thread thread2 = new Thread(four::m2);
Thread thread1 = new Thread(four::m1);
thread2.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread1.start();
System.out.println("准备打断");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread1.interrupt();
}
}
ReentrantLock 和 synchronized区别:
ReentrantLock:
1 可以设置公平锁/非公平锁
2 能实现synchronized的功能
3 可以进行尝试锁定,如果锁定怎么样,如果没锁定如何
4 可以被打断
Synchronized:非公平锁
Condition版本
public class MyThreadFive {
volatile List list = new LinkedList();
synchronized Object get(int num){
//如果list空了,等待拿去
while (getCount() == 0) {
try {
this.wait();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+" 在get中被打断了");
}
this.notifyAll(); //多线程中建议使用notifyAll 而不是notify
}
return list.remove(num);
}
synchronized void put(Object obj){
while (getCount() > 5){
try {
this.wait();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+" 在put中被打断了");
}
this.notifyAll();
}
list.add(obj);
}
synchronized int getCount(){
return list.size();
}
public static void main(String[] args) {
MyThreadFive myThreadFive = new MyThreadFive();
}
}
/**
* 使用lock版本
*/
class MyThreadFive1{
volatile List list = new LinkedList();
Lock lock = new ReentrantLock();
Condition prod = lock.newCondition();
Condition sumer = lock.newCondition();
Object get(int num){
//如果list空了,等待拿去
while (getCount() == 0) {
try {
sumer.await();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+" 在get中被打断了");
}
}
Object obj =list.remove(num);
prod.signalAll();
return obj;
}
void put(Object obj){
while (getCount() > 5){
try {
prod.await();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+" 在put中被打断了");
}
sumer.signalAll();
}
list.add(obj);
}
int getCount(){
return list.size();
}
}
ThreadLocal:线程局部变量
ThreadLocal<MyThreadFive> p = null;
自己的线程自己用,若A线程放入一个对象,则B线程获得不到这个对象
本文深入探讨并发编程核心,包括synchronized、ReentrantLock的使用与区别,公平锁、非公平锁概念,尝试锁定机制,以及如何利用Lock进行被打断的锁操作。同时,通过实例对比synchronized与ReentrantLock的特性,并介绍Condition条件变量的使用。
1842

被折叠的 条评论
为什么被折叠?



