有趣!
我玩了一些源代碼,看看有沒有條件斷點發生了什麼。附在下面。
執行與條件斷點調試器:
時間:1210623微秒
執行在調試器沒有條件斷點:
時間:24微秒
恕我直言VM不會停止,因爲第二個線程繼續並行運行。 Eclipse不得不將斷點代碼注入到當前類中。也許它在每次通話時都這樣做,也許它必須在每次通話時重新編譯班級。檢查Eclipse源將會揭示到底發生了什麼。我在C#和Visual Studio中運行條件斷點的經驗更糟糕:我的胃部感覺是事情在那裏差了好幾個數量級。
public class BreakPointPlay {
static int breakpointHits;
static volatile int modifiedBySecondThread;
static volatile boolean stopped;
public static void main(String[] args) throws InterruptedException {
Thread secondThread = startSecondThread();
final long LOOPS = 1000;
long counter = 0;
long start = System.nanoTime();
for (long i = 0; i < LOOPS; i++) {
// place breakpoint here and set the condition to the
// #breakPointCondition() method.
counter += i;
}
long stop = System.nanoTime();
long nanos = stop - start;
long micros = nanos/1000;
System.out.println("\nDuration: " + micros + " microseconds\n");
printInfo();
stopped = true;
secondThread.join();
}
private static Thread startSecondThread() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while(! stopped){
modifiedBySecondThread++;
}
}
});
thread.start();
return thread;
}
private static void printInfo() {
printModifiedBySecondThread();
printThread();
printClassLoader();
printStackTrace();
printModifiedBySecondThread();
}
private static void printStackTrace() {
Exception exception = new Exception();
exception.fillInStackTrace();
exception.printStackTrace(System.out);
}
private static void printModifiedBySecondThread() {
print("modifiedBySecondThread " + modifiedBySecondThread);
}
public static boolean breakPointCondition(){
breakpointHits++;
if(breakpointHits == 100){
printInfo();
}
return false;
}
private static void printClassLoader() {
print("ClassLoader " + new BreakPointPlay().getClass().getClassLoader());
}
private static void printThread() {
print("Thread " + Thread.currentThread());
}
private static void print(String msg){
System.out.println(msg);
}
}