码迷,mamicode.com
首页 > 编程语言 > 详细

多线程死锁代码

时间:2019-11-03 19:58:01      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:callback   new   cti   private   back   aging   except   cal   while   

package test;

import java.awt.List;
import java.awt.image.AreaAveragingScaleFilter;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.io.BufferedInputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import javax.security.auth.callback.LanguageCallback;
import javax.swing.text.StyledEditorKit.BoldAction;

class Lock {
	public static final Object alock = new Object();
	public static final Object block = new Object();
}

class Zhuzhu implements Runnable {
	public String c;
	private boolean flag;
	private int num = 100;

	public Zhuzhu(boolean flag) {
		this.flag = flag;
	}

	
	Object object = new Object();

	public void run() {
		if (flag == true) {
			while(true)
			synchronized (Lock.alock) {
				System.out.println("if...alock");
				synchronized (Lock.block) {
					System.out.println("if....block");
				}
			}
		}
		else {
			while(true)
			synchronized (Lock.block) {
				System.out.println("else...block");
				synchronized (Lock.alock) {
					System.out.println("else....alock");
				}
			}
		}
	}

}


public class Main {

	public static void main(String[] args) throws Exception {

		Scanner scanner = new Scanner(new BufferedInputStream(System.in));
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

		Zhuzhu c = new Zhuzhu(true);
		Zhuzhu d = new Zhuzhu(false);
		Thread t1 = new Thread(c);
		Thread t2 = new Thread(d);

		t1.start();
		t2.start();
		
	}
}

  

多线程死锁代码

标签:callback   new   cti   private   back   aging   except   cal   while   

原文地址:https://www.cnblogs.com/WINDZLY/p/11788619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!