籲請lock
或lockInterruptibly
會把線程WAITING
狀態:
線程狀態的等待線程。線程處於等待狀態,因爲調用下列方法之一:
- 的Object.wait不帶超時
- 的Thread.join沒有超時
- LockSupport.park
以下代碼啓動四個線程,前兩個(A,B)運行相同的代碼並通過lock
方法鎖定某個監視器。其他兩個(C,d)還運行相同的代碼,但它們經由lockInterruptibly
方法鎖定某些另一個監視器:
public static synchronized void dumpThreadState(List<Thread> threads) {
System.out.println("thread state dump start");
for (Thread t: threads) {
System.out.println(t.getName()+" "+t.getState());
}
System.out.println("thread state dump end\n");
}
public static void main(String[] args) throws InterruptedException {
final Lock lock = new ReentrantLock();
final Lock anotherLock = new ReentrantLock();
List<Thread> threads = new LinkedList<Thread>();
Runnable first = new Runnable() {
@Override
public void run() {
try {
lock.lock();
}
catch (Exception ex) {
System.out.println(Thread.currentThread().getName()+" processing exception "+ex.getClass().getSimpleName());
}
while (true);
}
} ;
Runnable second = new Runnable() {
@Override
public void run() {
try {
anotherLock.lockInterruptibly();
}
catch (InterruptedException ex) {
System.out.println(Thread.currentThread().getName()+" was interrupted");
}
while (true);
}
};
threads.add(new Thread(first,"A"));
threads.add(new Thread(first,"B"));
threads.add(new Thread(second,"C"));
threads.add(new Thread(second,"D"));
dumpThreadState(threads);
for (Thread t: threads) {
t.start();
}
Thread.currentThread().sleep(100);
dumpThreadState(threads);
System.out.println("interrupting " + threads.get(1).getName());
threads.get(1).interrupt();
dumpThreadState(threads);
System.out.println("interrupting " + threads.get(3).getName());
threads.get(3).interrupt();
Thread.currentThread().sleep(100);
dumpThreadState(threads);
for (Thread t: threads) {
t.join();
}
}
它輸出:
thread state dump start
A NEW
B NEW
C NEW
D NEW
thread state dump end
thread state dump start
A RUNNABLE
B WAITING
C RUNNABLE
D WAITING
thread state dump end
interrupting B
thread state dump start
A RUNNABLE
B WAITING
C RUNNABLE
D WAITING
thread state dump end
interrupting D
D was interrupted
thread state dump start
A RUNNABLE
B WAITING
C RUNNABLE
D RUNNABLE
thread state dump end
正如可以看出鎖定螺紋通過lock
方法不能中斷,而線程用lockInterruptibly
鎖定即可。
在另一個示例中,啓動了三個線程,前兩個線程(A,B)運行相同的代碼並通過塊鎖定在同一監視器上。
public static void main(String[] args) throws InterruptedException {
final Object lock = new Object();
final Object anotherLock = new Object();
List<Thread> threads = new LinkedList<Thread>();
Runnable first = new Runnable() {
@Override
public void run() {
synchronized(lock) {
while (true);
}
}
} ;
Runnable second = new Runnable() {
@Override
public void run() {
synchronized(anotherLock) {
try {
anotherLock.wait();
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
};
threads.add(new Thread(first,"A"));
threads.add(new Thread(first,"B"));
threads.add(new Thread(second,"C"));
dumpThreadState(threads);
for (Thread t: threads) {
t.start();
}
Thread.currentThread().sleep(100);
dumpThreadState(threads);
for (Thread t: threads) {
t.join();
}
}
它輸出:
thread state dump start
A NEW
B NEW
C NEW
thread state dump end
thread state dump start
A RUNNABLE
B BLOCKED
C WAITING
thread state dump end
線程C-結束了在WAITING
狀態,同時線程B在BLOCKING
狀態結束了:
另一個顯示器上的第三螺紋鎖而是經由
wait
方法等待
線程阻塞等待監視器鎖定的線程狀態。處於阻塞狀態的線程正在等待監視器鎖定,以便在調用Object.wait之後輸入同步塊/方法或重新輸入同步塊/方法。
編輯:
這裏是線程狀態的一個真正的好UML diagram。
對不起,我回復自動翻譯。我認爲你應該附上一個示例代碼,這將幫助我們幫助你分析問題。一般情況下使用同步,不需要鎖定。鎖 ()。一般來說,足夠同步。 –