1 wait和notify
首先需要注意的是wait和notify都不在thread里,而是在Object里为什么呢?
原因是因为:wait和notify的使用时针对锁的,如果没有synchronized,wait和notify也起不到线程通信的作用,而synchronized是对象锁,是针对对象的,所以wait和notify放在Object里面显得更合理。
- wait的作用
wait是让当前线程等待,特别需要注意的是wait之前肯定有synchronized也就是说当前线程肯定已经拿到锁了,wait之后线程会立刻释放锁 - notify的作用
notify的作用是唤醒之前wait的线程,需要注意的是就算notify也不是立刻释放锁,而是等到synchronized执行完了才释放锁
来看一个wait和notify的例子:
package com.yuxi;
/**
* Created by yuxi on 17/1/31.
*/
public class WaitDemo {
public static void main(String[] args) {
Object object = new Object();
MyThread myThread = new MyThread(object);
new Thread(myThread).start();
// new Thread(myThread).start();
//需要注意的是wait和notify都必须在synchronized里面,否则就会抛出异常
try {
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (object) {
System.out.println("因为wait释放锁了所有main线程可以立刻拿到" + Thread.currentThread().getName() + "..." + System.currentTimeMillis());
object.notify();
System.out.println("因为wait释放锁了所有main线程可以立刻拿到" + Thread.currentThread().getName() + "..." + System.currentTimeMillis());
}
}
static class MyThread implements Runnable {
private Object object;
public MyThread(Object object) {
this.object = object;
}
public void run() {
synchronized (object) {
try {
System.out.println("wait....." + Thread.currentThread().getName() + "..." + System.currentTimeMillis());
object.wait();
Thread.sleep(2000);
System.out.println("wait....." + Thread.currentThread().getName() + "..." + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
结果是:
wait.....Thread-0...1485911417379
因为wait释放锁了所有main线程可以立刻拿到main...1485911417380
因为wait释放锁了所有main线程可以立刻拿到main...1485911417380
wait.....Thread-0...1485911419384
从结果可以看出:
wait之后锁肯定释放,不然notify的synchronized是无法拿到锁的。
2 wait和notify实例
来模拟一个线程通信的实例:
package com.yuxi;
/**
* Created by yuxi on 17/2/1.
*/
public class TxDemo {
public static void main(String[] args) {
Object object = new Object();
String name = "yuxi";
ThreadA threadA = new ThreadA(object,null);
ThreadB threadB = new ThreadB(object,name);
new Thread(threadA).start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(threadB).start();
}
static class ThreadA implements Runnable {
private Object object;
private String name;
public ThreadA(Object object, String name) {
this.object = object;
this.name = name;
}
public void run() {
try {
synchronized (object) {
if (name == null) {
System.out.println( Thread.currentThread().getName() + "..." + System.currentTimeMillis());
object.wait();
System.out.println("..."+Thread.currentThread().getName()+".....wait");
System.out.println( Thread.currentThread().getName() + "..." + System.currentTimeMillis());
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class ThreadB implements Runnable {
private Object object;
private String name;
public ThreadB(Object object, String name) {
this.object = object;
this.name = name;
}
public void run() {
synchronized (object) {
if (name != null) {
System.out.println( Thread.currentThread().getName() + "..." + System.currentTimeMillis());
object.notify();
System.out.println("..."+Thread.currentThread().getName()+".....notify");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println( Thread.currentThread().getName() + "..." + System.currentTimeMillis());
}
}
}
}
}
例子很简单,一个线程A因为没有值处于等待状态,另外一个线程B拿到值之后唤醒线程A,使得两个线程直接完成通信。
结果:
Thread-0...1485913497377
Thread-1...1485913497481
...Thread-1.....notify
Thread-1...1485913502483
...Thread-0.....wait
Thread-0...1485913502483
3 wait和sleep,join区别
- 1 wait和yeild的区别
a: wait是让线程进入阻塞状态,yeild是让线程进入就绪状态
b: wait之后是会释放锁的,而yeild是不会释放锁的。 - 2 wait 和sleep 的区别
wait和sleep都是让线程进入阻塞状态,区别是wait会释放锁,而sleep不会释放锁。