Alex Haycock
Alex Haycock

Reputation: 395

Why won't this thread pause and loop?

I need this thread to loop slowly and pause for the duration of integerTime once the user presses buttonStart. I'm an amateur when it comes to Android and I have literally spent about 12 hours trying to get this to work. I would be very grateful if someone could help or correct my code. Cheers.

 final Button button = (Button) findViewById(R.id.buttonStart);             // Making the button to launch the
    button.setOnClickListener(new View.OnClickListener() {                     // actions below
        public void onClick(View v) {

            editTextTarget = (EditText)findViewById(R.id.editTextTarget);      // After user presses 'Start', it will
            stringTarget = editTextTarget.getText().toString();               // convert the input 'Target' into a string

            editTextPayload = (EditText)findViewById(R.id.editTextPayload);    // After user presses 'Start', it will
            stringPayload = editTextPayload.getText().toString();             // convert the input 'Payload' into a string and
            integerPayload = Integer.parseInt(stringPayload);                // then convert the string into an integer

            editTextTime =(EditText)findViewById(R.id.editTextTime);        // After user presses 'Start', it will
            stringTime = editTextTime.getText().toString();                // convert the input 'Time' into a string and
            integerTime = Integer.parseInt(stringTime);                   // then convert the string into an integer

            Thread threadBegin = new Thread()
            {
            public void run(){
                while (DOSrunning.get()) {
                try {
                    Thread.sleep(integerTime);               
                } catch (ClientProtocolException e) {
                } catch (IOException e) {
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }
                    }
            ;};

            threadBegin.start();

Upvotes: 1

Views: 188

Answers (1)

Gray
Gray

Reputation: 116878

You code looks fine to me. You are creating an anonymous class which extends Thread, overriding the run() method. You need to have an @Override before run() of course and I'm confused why you need the following catches unless you are cutting out some of the inner loop.

} catch (ClientProtocolException e) {
} catch (IOException e) {

I also don't see how DOSrunning() gets set. It looks like an AtomicBoolean which is good. Are you sure it is is initialized to true?

 AtomicBoolean DOSrunning = new AtomicBoolean(true);

If it is not that then the problem must be that the value in intergerTime is not milliseconds. A value of 1000 there will sleep 1 second.

Lastly, make sure to always handle the InterruptedException correctly. Something like this is a better pattern:

} catch (InterruptedException e) {
    // reset the interrupted flag
    Thread.currentThread().interrupt();
    e.printStackTrace();
    // quit the thread if necessary
    return;
}

Upvotes: 1

Related Questions