Trts
Trts

Reputation: 1069

Implementing Runnable and using Thread's methods within your own class

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.

Upvotes: 0

Views: 39

Answers (2)

Reimeus
Reimeus

Reputation: 159754

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

if (Thread.interrupted()) {

Read: Interrupts

Upvotes: 1

Arjit
Arjit

Reputation: 3456

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()

Upvotes: 0

Related Questions