Java 多线程协作 - 同步问题

一、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");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值