一、Thread类的几种状态
public enum State {
NEW,
RUNNABLE,
BLOCKED,
WAITING,
TIMED_WAITING,
TERMINATED;
}
二、Wait(), Notify() , NotifyAll()的使用
package p1;
public class A {
public static void main(String[] args) {
Car car = new Car();
new Thread(new CarRunnable1(car)).start();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(new CarRunnable2(car)).start();
}
}
class CarRunnable1 implements Runnable {
Car car;
public CarRunnable1(Car car) {
this.car = car;
}
@Override
public void run() {
car.test1();
}
}
class CarRunnable2 implements Runnable {
Car car;
public CarRunnable2(Car car) {
this.car = car;
}
@Override
public void run() {
car.test2();
}
}
class Car {
public synchronized void test1() {
try {
for (int index = 0; index < 20; index++) {
System.out.println("test1 index " + index);
Thread.sleep(300);
if(index == 10) {
wait();
notifyAll();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("test1 end");
}
public synchronized void test2() {
try {
for (int index = 0; index < 20; index++) {
System.out.println("test2 index " + index);
Thread.sleep(300);
if(index == 10) {
wait();
notifyAll();
// 注意写法的区别
// notifyAll();
// wait();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("test2 end");
}
}
注意:
1、wait():释放锁、线程处于wait状态,处于blocked状态的线程会竞争锁。测试发现wait状态下,notifyAll无效果;可以先notify,再wait()
2、notifyAll():通知block状态的线程获取锁,wait状态的线程则变成block状态(所有)
3、notify():通知block状态的线程获取锁,wait状态的线程则变成block状态(1个)
例子:
package p1;
public class C {
public static void main(String[] args) {
CarC car = new CarC();
Thread t1 = new Thread(new CarRunnableC1(car));
Thread t2 = new Thread(new CarRunnableC2(car));
Thread t3 = new Thread(new CarRunnableC3(car));
car.thread1 = t1;
car.thread2 = t2;
car.thread3 = t3;
t1.start();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
t3.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
}
}
class CarRunnableC1 implements Runnable {
CarC car;
public CarRunnableC1(CarC car) {
this.car = car;
}
@Override
public void run() {
car.test1();
}
}
class CarRunnableC2 implements Runnable {
CarC car;
public CarRunnableC2(CarC car) {
this.car = car;
}
@Override
public void run() {
car.test2();
}
}
class CarRunnableC3 implements Runnable {
CarC car;
public CarRunnableC3(CarC car) {
this.car = car;
}
@Override
public void run() {
car.test3();
}
}
class CarC {
Thread thread1;
Thread thread2;
Thread thread3;
public synchronized void test1() {
try {
for (int index = 0; index < 20; index++) {
System.out.println("test1 index " + index);
Thread.sleep(300);
if(index == 10) {
wait();
notifyAll();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("test1 end");
}
public synchronized void test2() {
try {
for (int index = 0; index < 20; index++) {
System.out.println("test2 index " + index);
Thread.sleep(300);
if(index == 10) {
Thread.sleep(500);
System.out.println("Thread 1:" + thread1.getState());
System.out.println("Thread 2:" + thread2.getState());
System.out.println("Thread 3:" + thread3.getState());
notify();
Thread.sleep(500);
System.out.println("Thread 1:" + thread1.getState());
System.out.println("Thread 2:" + thread2.getState());
System.out.println("Thread 3:" + thread3.getState());
wait();
Thread.sleep(500);
System.out.println("Thread 1:" + thread1.getState());
System.out.println("Thread 2:" + thread2.getState());
System.out.println("Thread 3:" + thread3.getState());
// 注意写法的区别
// notifyAll();
// wait();
//notifyAll();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("test2 end");
}
public synchronized void test3() {
try {
for (int index = 0; index < 20; index++) {
System.out.println("test3 index " + index);
Thread.sleep(300);
if(index == 10) {
wait();
// notifyAll();
// 注意写法的区别
// wait();
// notifyAll();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("test3 end");
}
}