JAVA多线程死锁的经典实例代码例子
正文:
JAVA多线程死锁的经典实例代码例子,如果对你有帮助就看看吧。实例代码:
class Tests implements Runnable{ private boolean flag; Tests(boolean flag){ this.flag = flag; } public void run(){ if (flag){ synchronized (MyLock.locka){ System.out.println("if locka"); synchronized (MyLock.lockb){ System.out.println("if lockb"); } } }else{ synchronized (MyLock.lockb){ System.out.println("else lockb"); synchronized (MyLock.locka){ System.out.println("else locka"); } } } } } class MyLock{ static Object locka = new Object(); static Object lockb = new Object(); } class DeadLockTest{ } public class test8 { public static void main(String[] args){ // Thread t = new Thread(new Tests(true)); Tests t = new Tests(true); Tests t2 = new Tests(false); Thread tt = new Thread(t); Thread tt2 = new Thread(t2); tt.start(); tt2.start(); } }
资源均来自第三方,谨慎下载,前往第三方网站下载