33
44import android .content .Context ;
55import android .graphics .*;
6- import android .os .Build ;
76import android .util .Log ;
87import android .view .MotionEvent ;
98import android .view .View ;
10- import androidx .annotation .RequiresApi ;
119import com .sergeiyarema .checkers .field .FieldController ;
1210
1311import 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
1517public 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