您的位置:首页 > 移动开发 > Objective-C

Monitor Object Monitors with Eclipse Object Monitor Monitors

2015-10-10 18:10 357 查看
package com.javalobby.tnt.threads;

public class ThreadTest {

private static final Object lockA = new Object();
private static final Object lockB = new Object();

public static void main(String[] args) {
Thread threadA = new Thread(getRunnableA(), "Thread A");
Thread threadB = new Thread(getRunnableB(), "Thread B");
threadA.start();
threadB.start();
}
private static Runnable getRunnableB() {
return new Runnable() {
public void run() {
synchronized(lockA) {
try {
Thread.sleep(500);
}
catch(InterruptedException e) { }
synchronized(lockB) {
System.out.println("Retrieved lock B and lock A");
}
}
}
};
}
private static Runnable getRunnableA() {
return new Runnable() {
public void run() {
synchronized(lockB) {
try {
Thread.sleep(500);
}
catch(InterruptedException e) { }

synchronized(lockA) {
System.out.println("Retrieved lock A and lock B");
}
}
}
};
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: