Skip to content

Commit c141f58

Browse files
committed
Change to use thread pools
1 parent a53259d commit c141f58

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

Checkers/app/src/main/java/com/sergeiyarema/checkers/GameActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ protected void onCreate(Bundle savedInstanceState) {
4040
}
4141

4242
private void startNewGame() {
43-
if (gameView != null && gameView.getBotThread() != null) {
44-
gameView.getBotThread().interrupt();
43+
if (gameView != null && gameView.getBotFuture() != null) {
44+
gameView.getBotFuture().cancel(true);
4545
}
4646
gameView = new GameView(this);
4747
gameLayout.removeAllViews();

Checkers/app/src/main/java/com/sergeiyarema/checkers/GameView.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33

44
import android.content.Context;
55
import android.graphics.*;
6-
import android.os.Build;
76
import android.util.Log;
87
import android.view.MotionEvent;
98
import android.view.View;
10-
import androidx.annotation.RequiresApi;
119
import com.sergeiyarema.checkers.field.FieldController;
1210

1311
import java.util.Arrays;
12+
import java.util.concurrent.ExecutorService;
13+
import java.util.concurrent.Future;
14+
15+
import static java.util.concurrent.Executors.newFixedThreadPool;
1416

1517
public class GameView extends View {
18+
private ExecutorService executor = newFixedThreadPool(5);
19+
1620
private static final int FIELD_SIZE = 8;
1721
private FieldController fieldController;
1822
private Context context;
19-
private Thread botThread;
23+
private Future botFuture;
2024

2125
public GameView(Context context) {
2226
super(context);
@@ -28,7 +32,7 @@ public GameView(Context context) {
2832
}
2933

3034
private void startBotThread() {
31-
botThread = new Thread(new Runnable() {
35+
botFuture = executor.submit(new Runnable() {
3236

3337
@Override
3438
public void run() {
@@ -39,17 +43,15 @@ public void run() {
3943
}
4044
}
4145
});
42-
botThread.start();
4346
}
4447

4548
private void assignTouchListener() {
4649
setOnTouchListener(new OnTouchListener() {
4750
public boolean onTouch(View v, MotionEvent event) {
48-
Thread th = new Thread(new OnTouchTask(event, fieldController));
49-
th.start();
51+
Future touchFuture = executor.submit(new OnTouchTask(event, fieldController));
5052
try {
51-
th.join();
52-
} catch (InterruptedException e) {
53+
touchFuture.get();
54+
} catch (Exception e) {
5355
Log.e("Touch exception", Arrays.toString(e.getStackTrace()));
5456
Thread.currentThread().interrupt();
5557
}
@@ -72,8 +74,8 @@ protected void onDraw(Canvas canvas) {
7274
fieldController.draw(canvas);
7375
}
7476

75-
public Thread getBotThread() {
76-
return botThread;
77+
public Future getBotFuture() {
78+
return botFuture;
7779
}
7880

7981
public FieldController getFieldController() {

0 commit comments

Comments
 (0)