Check the flag after a certain amount of time has passed since the thread started running without using the Timer thread.

What I wanted to do

In a thread that keeps running unless canceled from the outside, if the flag in the own thread is not set within 1 second or more after starting to run, I want to perform a special operation. Before

Go check the flag after 1 second with the timer task

――Since there are timer threads, the threads are scattered and somehow messed up. ――When the number of flags and fields increases, you have to think about exclusive processing one by one, which is very difficult.


public class AClass {
    private AThread aThread=new AThread();
    private volatile Handler mHandler;
    public void start(){
        aThread.start();
    }
    public void stop(){
        aThread.mIsStopThread=true;
    }
    private class AThread extends Thread{
        public volatile boolean mIsStopThread = false;
        volatile private boolean mIsGot=false;

        @Override
        public void run() {
           

            Timer mTimer = new Timer();
            mTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if ( !mIsGot&&!mIsStopThread) {
                                //If the desired data is not obtained even after 1 second and the thread is not canceled

                            }
                        }
                    });
                }
            }, 1000);

            while (!mIsStopThread){
                //Keeps moving unless the thread is canceled from the outside
                //When you get the data you want, set the mIsGot flag
            }
        }
    }
}

After Do not set the timer thread, record the time when the thread starts running, and judge whether 1 second has passed by looking at the difference from the current time.

--This prevents extra threads from being created and keeps the code clean.

public class AClass {
    private AThread aThread=new AThread();
    private volatile Handler mHandler;
    public void start(){
        aThread.start();
    }
    public void stop(){
        aThread.mIsStopThread=true;
    }
    private class AThread extends Thread{
        public volatile boolean mIsStopThread = false;
        volatile private boolean mIsGot=false;
        private long startTime;

        @Override
        public void run() {
            super.run();
            //Record Thread's first run time
            startTime = SystemClock.elapsedRealtime();

            while (!mIsStopThread){
                if (!mIsGot && (SystemClock.elapsedRealtime() - startTime) > 1000) {
                    //If the desired data is not obtained even after 1 second and the thread is not canceled
                }
                //Keeps moving unless the thread is canceled from the outside
                //When you get the data you want, set the mIsGot flag
                if(!mIsGot){
                //If you haven't taken it yet, put it to sleep
                   try {
                          Thread.sleep(100); 
                        } catch(InterruptedException e){
                          e.printStackTrace();
                        }  
                 }
            }
        }
    }
}

Recommended Posts

Check the flag after a certain amount of time has passed since the thread started running without using the Timer thread.
Check the status of Java application without using monitoring tool