多条件变量
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/*
多条件变量
**/
public class T {
private static ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) throws InterruptedException {
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
Thread t1 = new Thread(() -> {
try {
lock.lock();
condition1.await();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
});
Thread t1_1 = new Thread(() -> {
try {
lock.lock();
condition1.await();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
});
Thread t2 = new Thread(() -> {
try {
lock.lock();
condition2.await();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
});
Thread t3 = new Thread(() -> {
try {
lock.lock();
// condition1.signal(); //signal()只随机唤醒一个,一直阻塞
//signalAll()唤醒condition1上等待的所有线程
condition1.signalAll();
condition2.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
});
t1.start();
t1_1.start();
t2.start();
t3.start();
}
}
可打断
import java.util.concurrent.locks.ReentrantLock;
/*
可打断
**/
public class T {
private static ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
try {
System.out.println(Thread.currentThread().getName() + " 尝试获得锁");
lock.lockInterruptibly();
System.out.println(Thread.currentThread().getName() + " 线程获得了锁");
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(Thread.currentThread().getName() + " 线程没有获得锁,被打断...catch{return}");
} finally {
lock.unlock();
System.out.println(Thread.currentThread().getName() + " 释放锁");
}
}, "线程1");
System.out.println(Thread.currentThread().getName() + " 获得锁");
//main线程加锁原因:为了使线程1一直处于运行状态(此处即一直在获取main持有的锁)。
//假如main线程不加锁:线程1套层while(true)一直处于运行状态,再打断即可。
lock.lock();
thread.start();
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + "...");
//打断线程1
thread.interrupt();
System.out.println(Thread.currentThread().getName() + " 释放锁");
lock.unlock();
}
}
可重入
import java.util.concurrent.locks.ReentrantLock;
/**
可重入锁, 同一个线程可以多次获得锁对象
*/
public class T {
private static ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) {
m1();
}
private static void m1() {
try {
System.out.println("m1()...尝试获得锁");
lock.lock();
m2();
} finally {
System.out.println("m1()...释放锁");
lock.unlock();
}
}
private static void m2() {
try {
System.out.println("m2()...尝试获得锁");
lock.lock();
} finally {
System.out.println("m2()...释放锁");
lock.unlock();
}
}
}
可超时
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
/*
可超时
**/
public class T {
private static ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
try {
System.out.println(Thread.currentThread().getName() + " 尝试获得锁");
//500ms必须获得锁,否则IllegalMonitorStateException
lock.tryLock(500, TimeUnit.MILLISECONDS);
System.out.println(Thread.currentThread().getName() + " 成功获得锁");
} catch (Exception e) {
System.out.println(Thread.currentThread().getName() + " 获取锁失败");
e.printStackTrace();
} finally {
System.out.println(Thread.currentThread().getName() + "释放了锁");
lock.unlock();
}
}, "线程1");
System.out.println(Thread.currentThread().getName() + " 获得锁");
lock.lock();
t1.start();
//主线程1000ms执行完才能释放锁,线程1超时异常
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + "释放了锁");
lock.unlock();
}
}