0

They say that implementing Runnable is more preferrable than extending Thread. I agree that this is more Java-like.

Ok. I would like to do something like this:

public class HotThread implements Runnable {
...
    private void execute() {
        if (this.isInterrupted()) {
        ....
        }    
    }
}

So, now I get an error as there is no method isInterrupted. Could you help me understand this moment: how to use methods of the class Thread within your own class in this case.

2 Answers 2

1

isInterrupted is defined for the Thread but for the current thread you could use the shorthand form

if (Thread.interrupted()) {

Read: Interrupts

Sign up to request clarification or add additional context in comments.

Comments

0

It is defined in Thread class - java.lang.Thread.isInterrupted()

To use this

HotThread  ht = new HotThread();
Thread t = new Thread(ht);

use t.isInterrupted()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.