java并发

This commit is contained in:
luoxiang
2019-11-26 23:28:26 +08:00
parent 3884f30ffd
commit fdb2e9404c
3 changed files with 152 additions and 47 deletions

View File

@ -17,9 +17,9 @@ public class AwaitAndSignal {
try {
lock.lock();
String threadName = Thread.currentThread().getName();
System.out.println(threadName + "等待通知...");
System.out.println(threadName + "线程等待通知...");
condition.await();
System.out.println(threadName + "获得锁");
System.out.println(threadName + "线程后续操作");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
@ -31,9 +31,10 @@ public class AwaitAndSignal {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(new IncreaseTask());
thread1.start();
Thread.sleep(2000);
// 必须要再次获取该重入锁否则会抛出IllegalMonitorStateException异常
Thread.sleep(1000);
System.out.println("主线程开始操作");
lock.lock();
System.out.println("主线程唤醒");
condition.signal();
lock.unlock();
}

View File

@ -36,7 +36,6 @@ public class J5_NotifyAll {
System.out.println("对象object唤醒");
// 如果是object.notify()则是随机唤醒任意一个等待
object.notifyAll();
}
}).start();
}