File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed
src/main/java/kms/chapter02 Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ package kms .chapter02 ;
2+
3+ import java .util .concurrent .atomic .AtomicInteger ;
4+
5+ public class Point {
6+ private final AtomicInteger x = new AtomicInteger (0 );
7+ private final AtomicInteger y = new AtomicInteger (0 );
8+
9+ void rightUp () {
10+ x .incrementAndGet ();
11+ y .incrementAndGet ();
12+ }
13+
14+ int getX () {
15+ return x .get ();
16+ }
17+
18+ int getY () {
19+ return y .get ();
20+ }
21+
22+ @ Override
23+ public String toString () {
24+ return String .format ("%d, %d" , getX (), getY ());
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package kms .chapter02 ;
2+
3+ import java .util .concurrent .ExecutorService ;
4+ import java .util .concurrent .Executors ;
5+ import java .util .concurrent .Future ;
6+
7+ public class PointSample {
8+
9+ public static void main (String [] args ) throws Exception {
10+ final Point pos = new Point ();
11+
12+ Runnable task = () -> {
13+ for (int i = 0 ; i < 10000 ; i ++) {
14+ pos .rightUp ();
15+ }
16+ };
17+
18+ ExecutorService executorService = Executors .newCachedThreadPool ();
19+
20+ Future <Boolean > future1 = executorService .submit (task , true );
21+ Future <Boolean > future2 = executorService .submit (task , true );
22+
23+ if (future1 .get () && future2 .get ()) {
24+ System .out .println (pos );
25+ } else {
26+ System .out .println ("failed" );
27+ }
28+
29+ executorService .shutdown ();
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments