Skip to content

Commit e25bcc7

Browse files
committed
[2장] Point 예제 Java 로 테스트
1 parent 31362f1 commit e25bcc7

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

0 commit comments

Comments
 (0)