File tree Expand file tree Collapse file tree 7 files changed +129
-0
lines changed
src/main/kotlin/ezhoon/chapter02 Expand file tree Collapse file tree 7 files changed +129
-0
lines changed Original file line number Diff line number Diff line change 1+ package ezhoon.chapter02
2+
3+ import java.util.concurrent.atomic.AtomicInteger
4+
5+ class AtomicConsumer {
6+
7+ @Volatile
8+ var count = AtomicInteger (0 )
9+ private set
10+
11+ fun increment () {
12+ count.incrementAndGet()
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package ezhoon.chapter02
2+
3+ import java.util.concurrent.Executors
4+
5+ fun main () {
6+
7+ val counter = AtomicConsumer ()
8+
9+ val task = Runnable {
10+ for (i in 0 .. 9999 ) {
11+ counter.increment()
12+ }
13+ }
14+
15+ val executorService = Executors .newCachedThreadPool()
16+
17+ val future1 = executorService.submit(task, true )
18+ val future2 = executorService.submit(task, true )
19+
20+ if (future1.get() && future2.get()) {
21+ println (" ${counter.count} " )
22+ } else {
23+ println (" 실패" )
24+ }
25+
26+ executorService.shutdown()
27+ }
Original file line number Diff line number Diff line change 1+ package ezhoon.chapter02
2+
3+ import java.util.concurrent.Executors
4+
5+ fun main () {
6+
7+ val point = Point ()
8+
9+ val task = Runnable {
10+ for (i in 0 .. 9999 ) {
11+ point.rightUp()
12+ }
13+ }
14+
15+ val executorService = Executors .newCachedThreadPool()
16+
17+ val future1 = executorService.submit(task, true )
18+ val future2 = executorService.submit(task, true )
19+ val future3 = executorService.submit(task, true )
20+ val future4 = executorService.submit(task, true )
21+ val future5 = executorService.submit(task, true )
22+ val future6 = executorService.submit(task, true )
23+
24+ if (future1.get() && future2.get() && future3.get() && future4.get() && future5.get() && future6.get()) {
25+ println (" ${point.getX()} ,${point.getY()} " )
26+ } else {
27+ println (" 실패" )
28+ }
29+
30+ executorService.shutdown()
31+ }
Original file line number Diff line number Diff line change 1+ package ezhoon.chapter02
2+
3+ import java.util.concurrent.atomic.AtomicInteger
4+
5+ class Point {
6+
7+ private val x = AtomicInteger (0 )
8+ private val y = AtomicInteger (0 )
9+
10+ fun rightUp () {
11+ x.incrementAndGet()
12+ y.incrementAndGet()
13+ }
14+
15+ fun getX () = x.get()
16+
17+ fun getY () = y.get()
18+ }
Original file line number Diff line number Diff line change 1+ package ezhoon.chapter02
2+
3+ class Consumer {
4+
5+ @Volatile
6+ var count: Int = 0
7+ private set
8+
9+ fun increment () {
10+ count++
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ package ezhoon.chapter02
2+
3+ import java.util.concurrent.Executors
4+
5+ fun main () {
6+
7+ val counter = Consumer ()
8+
9+ val task = Runnable {
10+ for (i in 0 .. 9999 ) {
11+ counter.increment()
12+ }
13+ }
14+
15+ val executorService = Executors .newCachedThreadPool()
16+
17+ val future1 = executorService.submit(task, true )
18+ val future2 = executorService.submit(task, true )
19+
20+ if (future1.get() && future2.get()) {
21+ println (" ${counter.count} " )
22+ } else {
23+ println (" 실패" )
24+ }
25+
26+ executorService.shutdown()
27+ }
You can’t perform that action at this time.
0 commit comments