资源抢占 导致死锁
public static void main(String[] args) {
final Object a = new Object();
final Object b = new Object();
Thread threadA = new Thread(new Runnable() {
public void run() {
synchronized (a) {
try {
System.out.println("now i in threadA-locka");
Thread.sleep(1000l);
synchronized (b) {
System.out.println("now i in threadA-lockb");
}
} catch (Exception e) {
// ignore
}
}
}
});
Thread threadB = new Thread(new Runnable() {
public void run() {
synchronized (b) {
try {
System.out.println("now i in threadB-lockb");
Thread.sleep(1000l);
synchronized (a) {
System.out.println("now i in threadB-locka");
}
} catch (Exception e) {
// ignore
}
}
}
});
threadA.start();
threadB.start();
}
第二种写法 思路都一样
public static void miin(String[] args){
final Object lockA = new Object();
final Object lockB = new Object();
Thread threadA = new Thread(new Runable{
public void run(){
synchnaized(lockA){
Sleep(1000);
synchnaized(LockB){
}
}
}
});
Thread threadB = new Thread(new Runable{
public void run(){
synchnaized(lockB){
Sleep(1000);
synchnaized(LockA){
}
}
}
});
threadA.start();
threadB.start();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)