---
# System prepended metadata

title: 死結的範例
tags: [ocpjp]

---

# 死結的範例
```java=
public class Counter extends Thread{
	int i = 10;
	public synchronized void display(Counter obj) {
		try {
			Thread.sleep(5);
			obj.increament(this);
			System.out.println(i);
		}catch(InterruptedException ex) {
			
		}
	}
	public synchronized void increament(Counter obj) {
		i++;
	}
	public static void main(String[] args) {
		final Counter obj1 = new Counter();
		final Counter obj2 = new Counter();
		new Thread(new Runnable() {
			public void run() {
				obj1.display(obj2);
			}
		}).start();
		new Thread(new Runnable() {
			public void run() {
				obj2.display(obj1);
			}
		}).start();
	}
}
```
## From what threading problem does the program suffer?
A. race condition
**B. deadlock**
C. starvation
D. livelock
- [x] **Answer: B**

###### tags: `ocpjp`