0
0

【初めてのJava】まずはコーディングしてみた(スレッド / その1)

Last updated at Posted at 2024-01-16

はじめに

スレッドの実装練習として、カウントダウンするタイマー機能を作ってみました。
タイマーは標準のものは使わず、自作します。

機能

指定した数値からスタートして、1秒ごとにカウントダウンするだけの機能です。

ソースコード

App.java
/*
*/
import java.util.Scanner;
public class App{
    public static void main(String[] args){
        try(Scanner scanner = new Scanner(System.in)){
            
            System.out.print("カウントダウン値入力 ");
            int num = scanner.nextInt();

            CountDownThread countDownThread = new CountDownThread();
            countDownThread.start();
            if(!countDownThread.setTimerCount(num)){
                countDownThread.stopThread();
                System.out.println("Wrong argument.");
                return;
            }
            countDownThread.startCountDown();
            while(!countDownThread.getTimeUpFlg()){
                Thread.sleep(1000);
            }
            countDownThread.stopThread();
            System.out.println("Time's Up!");
        }catch(Exception e){
            System.out.println("Exception issues");
            System.out.println(e.getMessage());
        }
    }
}
CountDownThread.java
public class CountDownThread extends Thread{
    public enum TIMER_STATE{
        NOT_SET,
        EXECUTABLE,
        IN_PROCESS,
        STOP
    }
    private TIMER_STATE state = TIMER_STATE.NOT_SET;
    private int timeCount = 0;
    private boolean timeUpFlg = false;
    private boolean threadRun = true;

    public void run(){
        try{
            mainloop();
        }catch(Exception e){
            System.err.println("Timer thread is closed");
        }
    }
    public boolean setTimerCount(int timeCount){
        if((this.state == TIMER_STATE.EXECUTABLE) || (this.state == TIMER_STATE.STOP)){
            return false;
        }
        if(( 0 >= timeCount) && (timeCount >= 3600)){
            return false;
        }
        this.timeCount = timeCount;
        this.state = TIMER_STATE.EXECUTABLE;
        return true;
    }

    public boolean startCountDown(){
        if((this.state == TIMER_STATE.NOT_SET) || (this.state == TIMER_STATE.IN_PROCESS)){
            return false;
        }
        this.state = TIMER_STATE.IN_PROCESS;
        return true;
    }

    public boolean stopCountDown(){
        if((this.state == TIMER_STATE.NOT_SET) || (this.state == TIMER_STATE.EXECUTABLE)){
            return false;
        }
        this.state = TIMER_STATE.STOP;
        return true;
    }
    
    public void cancel(){
        this.state = TIMER_STATE.NOT_SET;
        this.timeCount = 0;
        this.timeUpFlg = false;
    }

    public boolean getTimeUpFlg(){
        return this.timeUpFlg;
    }

    private void count1s(){
        try{
            sleep(1000);
        }catch(InterruptedException e){

        }
    }

    public void stopThread(){
        this.threadRun = false;
    }

    private void mainloop(){
        while(threadRun){
            if((this.state == TIMER_STATE.IN_PROCESS) && ( this.timeCount > 0 )){
                count1s();
                this.timeCount--;
                System.out.println(this.timeCount);
                if(this.timeCount <= 0){
                    this.state = TIMER_STATE.NOT_SET;
                    this.timeUpFlg = true;
                }
            }else{
                // Wait Start
                count1s();
            }
        }
    }
}

結果

$java App 
カウントダウン値入力 5
4
3
2
1
0
Time's Up!

感想

改善のしがいがアリそうなコードを書いた気がします。
スレッドはとても奥が深い概念でマスターするのに時間がかかりそうです。
まずはアウトプットしたかったので いま考えられる範囲で書いてみました。
そしてもう少し精進したいなと思いました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0