---
# System prepended metadata

title: AtomicInteger的用法
tags: [ocpjp]

---

# AtomicInteger的用法
[參考網站](https://magiclen.org/ocpjp-atomicinteger/)

```java=
import java.util.concurrent.atomic.AtomicInteger;

class MyThread implements Runnable {
	private static AtomicInteger count = new AtomicInteger (0);
	public void run () {
		int x = count.incrementAndGet();
		System.out.print (x+" ");
	}
}
public class Test {

	public static void main(String[] args) {
		
		Thread thread1 = new Thread(new MyThread());
		Thread thread2 = new Thread(new MyThread());
		Thread thread3 = new Thread(new MyThread());
		
		Thread [] ta = {thread1, thread2, thread3};
		for (int x= 0; x < 3; x++) {
			ta[x].start();
		}
	}

}
```

Console:
```=
1 3 2 
```
}
Which statement is true?
**A. The program prints 1 2 3 and the order is unpredictable.**
B. The program prints 1 2 3.
C. The program prints 1 1 1.
D. A compilation error occurs.
- [x] **Answer: A**

###### tags: `ocpjp`
