public static void main(String[] args) {
// surprising
// Compile only iconst_1
System.out.println(("hello123" == ("hel" + "lo" + 123)));
Thread t = new Thread() {
public void run() {
System.out.println("X: " + Thread.currentThread().getState());
}
};
System.out.println(t.getState());
t.start();
t = new Thread() {
private Object lock = new Object();
public void run() {
synchronized (lock) {
try {
System.out.println("Getting into lock.wait()");
lock.wait(2000);
} catch (InterruptedException interruptedEx) {
interruptedEx.printStackTrace();
}
}
}
};
t.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(t.getState());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
System.out.println(t.getState());
final Object sharedLock = new Object();
Thread anotherThreadHoldsTheLockForAWhile = new Thread() {
public void run() {
synchronized (sharedLock) {
System.out.println(getName() + " holding sharedLock");
try {
Thread.sleep(50000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
anotherThreadHoldsTheLockForAWhile.start();
holdASec();
System.out.println("XX: " + anotherThreadHoldsTheLockForAWhile.getState());
t = new Thread() {
public void run() {
synchronized (sharedLock) {
System.out.println("Got the lock");
}
}
};
t.start();
holdASec();
System.out.println("YY: " + t.getState());
}
private static void holdASec() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
如题,上面是我按照下面网址参考写的测试代码,仅仅是测试。。。。
http://www.jguru.com/faq/view.jsp?EID=1253344
http://shihaiyang.iteye.com/blog/437902
http://www.science.uva.nl/ict/ossdocs/java/tutorial/java/threads/states.html
http://blog.csdn.net/westwin/article/details/4658602
http://www.herongyang.com/Java-Tools/jstack-jhat-Java-Heap-Analysis-Tool.html
http://stackoverflow.com/questions/7497793/understanding-java-lang-thread-state-waiting-parking
-------------------------------------------------------------------------------
Thread.sleep() sends the current thread into the "Not Runnable" state for some amount of time. The thread keeps the monitors it has aquired -- i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread.
Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.
t.suspend() is deprecated. Using it is possible to halt a thread other than the current thread. A suspended thread keeps all its monitors and since this state is not interruptable it is deadlock prone.
-------------------------------------------------------------------------------