1. wait和notify方法
wait()
方法可以使当前执行代码的线程进行等待,notify()
方法用来通知那些等待该对象锁的其它线程。wait()
和notify()
方法必须要获得该对象的锁,才能运行,否则会报IllegalMonitorStateException
的异常。
try {
Object lock = new Object();
synchronized (lock) {
lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
2. wait方法
调用wait()
方法后,会自动放弃获得的对象锁供其他使用,直到其它线程将其唤醒。而wait(long)
方法,在等待指定时间后,自动进入就绪状态,重新运行。
public class ThreadWait {
public static void main(String[] args) {
new ThreadA().start();
new ThreadB().start();
}
static class ThreadA extends Thread {
public void run() {
Object lock = new Object();
synchronized (lock) {
try {
System.out.println("In ThreadA begin at " + System.currentTimeMillis());
lock.wait();
System.out.println("In ThreadA end at " + System.currentTimeMillis());
} catch (InterruptedException e) {
}
}
}
}
static class ThreadB extends Thread {
public void run() {
Object lock = new Object();
synchronized (lock) {
try {
System.out.println("In ThreadB begin at " + System.currentTimeMillis());
lock.wait(2000);
System.out.println("In ThreadB end at " + System.currentTimeMillis());
} catch (InterruptedException e) {
}
}
}
}
}
输出
线程B在2秒后结束,而线程A一直没有结束
3. notify和notifyAll方法
notify()
方法也只能在同步方法或者同步代码块中运行,notify()
并不会立即释放该对象锁,直到线程将程序执行完。
public class ThreadNotify {
public static void main(String[] args) {
Object lock = new Object();
new ThreadA(lock).start();
new ThreadB(lock).start();
}
static class ThreadA extends Thread {
Object lock;
ThreadA(Object lock) {
this.lock = lock;
}
public void run() {
synchronized (lock) {
try {
System.out.println("In ThreadA begin at " + System.currentTimeMillis());
lock.wait();
System.out.println("In ThreadA end at " + System.currentTimeMillis());
} catch (InterruptedException e) {
}
}
}
}
static class ThreadB extends Thread {
Object lock;
ThreadB(Object lock) {
this.lock = lock;
}
public void run() {
synchronized (lock) {
System.out.println("In ThreadB begin at " + System.currentTimeMillis());
lock.notify();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
System.out.println("In ThreadB end at " + System.currentTimeMillis());
}
}
}
}
输出
notify()
方法只会唤醒等待队列中的一个线程,如果需要所有线程需要调用notifyAll()
。
public static void main(String[] args) {
Object lock = new Object();
new ThreadA(lock).start();
new ThreadA(lock).start();
new ThreadB(lock).start();
}
输出
只有一个ThreadA
被唤醒,另一个仍然在等待中。
4. wait和interrupt方法
如果线程进入wait()
状态,调用线程的interrupt()
方法会发生InterruptedException
异常。
public class ThreadInterruptWait {
public static void main(String[] args) {
try {
Object lock = new Object();
ThreadA threadA = new ThreadA(lock);
threadA.start();
Thread.sleep(1000);
threadA.interrupt();
} catch (InterruptedException e) {
}
}
static class ThreadA extends Thread {
private Object lock;
public ThreadA(Object lock) {
this.lock = lock;
}
public void run() {
System.out.println("In ThreadA start at " + System.currentTimeMillis());
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("In ThreadA end at " + System.currentTimeMillis());
}
}
}
输出
相关文章
Java Thread.wait和Thread.notify方法
Java Thread.join方法
Java Thread.sleep方法
Java Thread.Interrupt方法
Java Lock对象