public class Concurrent3 {
static Object obj = new Object();
static Lock lock = new ReentrantLock();
public static void main(String[] args) {
for(int i = 0 ; i < 10 ; i++){
test1();
}
}
private static void test1() {
Thread t = new Thread(() -> {
synchronized (obj){
try {
System.out.println("hello");
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
}
});
t.start();
}
public static void test2(){
Thread t = new Thread(() -> {
try {
lock.lock();
System.out.println("hello");
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
});
t.start();
}
}
运行test1()和test2()
都是每隔一秒打印一句Hello
lock.tryLock();表示尝试获取锁
成功获取的话返回true
获取失败返回false
public static void test3(){
Thread t = new Thread(() -> {
boolean b = false;
try {
b = lock.tryLock();
if(b){
System.out.println("hello");
Thread.sleep(1000);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(b) lock.unlock();
}
});
t.start();
}
原因:线程1获取了锁成功,b==true
其他线程获取锁失败所以就没有执行输出语句
public static void test3(){
Thread t = new Thread(() -> {
boolean b = false;
try {
b = lock.tryLock(1500L, TimeUnit.MILLISECONDS);
if(b){
System.out.println("hello");
Thread.sleep(1000);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(b) lock.unlock();
}
});
t.start();
}
obj.wait();//表示释放锁,
obj.wait():表示拿了锁然后把他释放掉,进入阻塞,等待被唤醒
obj.notify():唤醒一个调用wait()方法进入阻塞的线程,前提是你也占用了锁资源
static Object obj = new Object();
static Lock lock = new ReentrantLock();
public static void main(String[] args) throws InterruptedException {
for(int i = 0 ; i < 10 ; i++){
test1();
}
Thread.sleep(2000);
obj.notify();
}
private static void test1() {
Thread t = new Thread(() -> {
synchronized (obj){
try {
obj.wait();
System.out.println("hello");
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
}
});
t.start();
}
public static void test2(){
Thread t = new Thread(() -> {
try {
lock.lock();
System.out.println("hello");
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
});
t.start();
}
执行会报错
修改:
static Object obj = new Object();
static Lock lock = new ReentrantLock();
public static void main(String[] args) throws InterruptedException {
for(int i = 0 ; i < 10 ; i++){
test1();
}
Thread.sleep(2000);
synchronized (obj){
obj.notify();
}
}
private static void test1() {
Thread t = new Thread(() -> {
synchronized (obj){
try {
obj.wait();
System.out.println("hello");
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
}
});
t.start();
}
public static void test2(){
Thread t = new Thread(() -> {
try {
lock.lock();
System.out.println("hello");
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
});
t.start();
}
唤醒了一个阻塞线程,其他都在阻塞
obj.notifyAll();
唤醒所以
static Object obj = new Object();
static Lock lock = new ReentrantLock();
public static void main(String[] args) throws InterruptedException {
for(int i = 0 ; i < 10 ; i++){
test1();
}
Thread.sleep(2000);
synchronized (obj){
obj.notifyAll();
}
}
private static void test1() {
Thread t = new Thread(() -> {
synchronized (obj){
try {
obj.wait();
System.out.println("hello");
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
}
});
t.start();
}
lock如何释放和唤醒锁
static Lock lock = new ReentrantLock();
Condition c1 = lock.newCondition();