rajvi
rajvi

Reputation: 3

Thread stop and restart

I have issues as follows:

  1. I am working with threads in android and I want to run a thread when the activity starts. After a condition again I need to start the same thread in the same activity.
  2. I searched so many sites but all say that I need to make instance of the same thread and I did it also. But of no help. Can anyone help?

Upvotes: 0

Views: 1636

Answers (3)

GEverding
GEverding

Reputation: 3201

I've never down multithreading in andriod land but from what you described you need to have a loop with associated blocking mechanisms. Look at this sight to get an overview of multithreading.

Upvotes: 0

Nikunj Patel
Nikunj Patel

Reputation: 22066

i think you need a countdownTimer, which working as loop

public void looper(final MediaPlayer secAudio) {
    try
    {
    tns_DCounter = new CountDownTimer(2000, 1000) {
        public void onTick(long millisUntilFinished) {
            try
            {
            mPlayer.start();
            }catch (Exception e) {

            }
        }

        public void onFinish() {
            try
            {
            secAudio.start();
            tns_DCounter2 = new CountDownTimer(3000, 1000) {
                public void onTick(long millisUntilFinished) {

                }
                @Override
                public void onFinish() {
                    looper(secAudio);
                }

            };
            tns_DCounter2.start();
            }catch (Exception e) {
                // TODO: handle exception
            }
        }
    };
    tns_DCounter.start();
    }catch (Exception e) {
    }

}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500035

You can't restart a thread. The closest you can come is to make the thread pause somehow (e.g. wait on a monitor) and then resume it later (e.g. by notifying that monitor). But once a thread has actually stopped, it can't be restarted.

Upvotes: 1

Related Questions