说明:参考《Java多线程核心技术》
3、线程之间的通信
1、wait/notify
```public class WaitThread extends Thread{
private Object lock;
public WaitThread(Object lock) {
super();
this.lock = lock;
}
@Override
public void run() {
try {
synchronized (lock) {
System.out.println("wait start time = " + System.currentTimeMillis());
lock.wait();
System.out.println("wait end time = " + System.currentTimeMillis());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class NotifyThread extends Thread {
private Object lock;
public NotifyThread(Object lock) {
super();
this.lock = lock;
}
@Override
public void run() {
synchronized (lock) {
System.out.println("notify start time = " + System.currentTimeMillis());
lock.notify();
System.out.println("notify end time = " + System.currentTimeMillis());
}
}
}
public class WaitNotifyTest {
public static void main(String[] args) {
try {
Object lock = new Object();
WaitThread waitThread = new WaitThread(lock);
waitThread.start();
Thread.sleep(2000);
NotifyThread notifyThread = new NotifyThread(lock);
notifyThread.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
wait start time = 1546782925678
notify start time = 1546782927691
notify end time = 1546782927691
wait end time = 1546782927691
*/
方法wait()的作用和是使当前执行代码的进程进行等待,是Object类中的方法,作用是将当前线程置入“预执行队列”中,并且在wait()所在的代码行处停止执行,直到收到通知或者被中断为止。在调用wait()方法之前,线程必须获得该对象的的对象级别锁,即只能在同步方法或同步块中调用wait()方法。在执行wait()方法之后,当前线程释放锁。
方法notify()用来通知那些可能等待该对象的对象锁的其他线程,如果有多个线程等待,则由线程规划器随机挑出其中一个呈wait()状态的线程,对其发出notify通知,并使得它等待获取该对象的对象锁。需要说明的是,在执行notify()方法后,当前线程不会马上释放该对象的锁,呈wait()状态的线程也不能马上获得该对象锁,要等到执行notify()方法的线程将程序执行完,也就是退出synchronized代码块后,当前的线程才会释放锁,而呈wait()状态的线程才可以获得该对象锁。
public class AddList {
private static List list = new ArrayList();
public static void add() {
list.add("add String");
}
public static int size() {
return list.size();
}
}
public class WaitThread extends Thread{
private Object lock;
int num = 3;
public WaitThread(Object lock) {
super();
this.lock = lock;
}
@Override
public void run() {
try {
synchronized (lock) {
if (AddList.size() != num) {
System.out.println("wait thread start time = " + System.currentTimeMillis());
lock.wait();
System.out.println("wait Thread end time = " + System.currentTimeMillis());
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class NotifyThread extends Thread{
private Object lock;
int num = 5;
public NotifyThread(Object lock) {
super();
this.lock = lock;
}
@Override
public void run() {
synchronized (lock){
try {
for (int i = 0; i < num; i++){
AddList.add();
if (AddList.size() == 3) {
lock.notify();
System.out.println("notify has send...");
}
System.out.println("add " + (i + 1) + " elements...");
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class WaitNotifyTest {
public static void main(String[] args) {
try {
Object lock = new Object();
WaitThread waitThread = new WaitThread(lock);
waitThread.start();
Thread.sleep(50);
NotifyThread notifyThread = new NotifyThread(lock);
notifyThread.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
wait thread start time = 1546788953862
add 1 elements...
add 2 elements...
notify has send...
add 3 elements...
add 4 elements...
add 5 elements...
*/
这个例子说明:在notify方法执行后,并不是立即释放当前持有的锁。
2、线程的执行和状态
1、新创建一个新的线程对象后,再调用它的start()方法,系统会为此线程分配CPU资源,使其处于Runnable状态。如果此线程抢到CPU资源,那么此线程就处于Running状态。
2、Runnable状态和Running Man状态是可以相互转换的,其中的原因有运行中将CPU资源让给高优先级的线程先执行。
3、Block就是阻塞的意思,Block结束后,进入Runnable状态,等待系统重新分配资源。
出现阻塞的原因:
1、线程调用sleep方法,主动放弃当前占用的资源。
2、线程调用了阻塞式IO方法,在该方法返回前,该线程一直被阻塞。
3、线程视图获得一个同步监视器,但是该同步监视器被其他的线程锁持有。
4、线程等待某个通知。
5、程序调用了suspend方法,致使当前的线程被挂起。
4、run()方法运行结束后进入销毁阶段,整个线程执行完毕。
说明:每个所对象都有两个队列,一个是就绪队列,一个是阻塞队列。就绪队列储存了将要获得资源的线程,阻塞队列存储被阻塞的线程。
3、interrupt遇到wait
public class ThreadA extends Thread{
private Object lock;
public ThreadA(Object lock) {
super();
this.lock = lock;
}
@Override
public void run() {
InterruptClass interClass = new InterruptClass();
interClass.interruptService(lock);
}
}
public class InterruptClass {
public void interruptService(Object lock) {
try {
synchronized (lock) {
System.out.println("-- start wait()");
lock.wait();
System.out.println("-- end wait()");
}
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("release lock");
}
}
}
public class InterruptNotifyTest {
public static void main(String[] args) {
try {
Object lock = new Object();
ThreadA threadA = new ThreadA(lock);
threadA.start();
Thread.sleep(3000);
threadA.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
-- start wait()
release lock
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
*/
说明:必须执行完notify()方法所在的同步synchronized代码块后才能释放锁。
上述结果说明:如果wait()方法遇到interrupt()方法时不释放锁,那么一定会输入-- end wait()这个结果的。在执行同步代码快的过程中,遇到异常而导致终止,锁也会被释放。执行完同步代码块后就会释放对象的锁
4、wait的其他情况
wait(long):带一个参数的wait(long)的功能是等待某一时间内释放有线程对锁进行唤醒,如果超过这个时间,则自动唤醒。
通知过早情况,如果通知过早,那么wait()方法没必要继续执行了