学习笔记(03):19年并发编程及原理视频培训教程入门到精通-并发编程的挑战之死锁...

立即学习:https://edu.csdn.net/course/play/9827/208780?utm_source=blogtoedu


public class test1 {
	private  static final Object hair_a=new Object();
	private  static final Object hair_b=new Object();
	public static void main(String[] args) {
		Thread thread1 = new Thread(new Runnable(){
			public void run() {
				// TODO Auto-generated method stub
				synchronized (hair_a) {
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					synchronized (hair_b) {
						System.out.println("a抓到了b");
					}
				}
			}
			
		});
		Thread thread2 = new Thread(new Runnable(){
			public void run() {
				// TODO Auto-generated method stub
				synchronized (hair_b) {
					synchronized (hair_a) {
						System.out.println("b抓到了a");
					}
				}
			}
			
		});
		thread1.start();
		thread2.start();
	}
}
发布了7 篇原创文章 · 获赞 6 · 访问量 34

猜你喜欢

转载自blog.csdn.net/weixin_45831970/article/details/104357592