non-stop thread

P

Peter

Hi
I have a class that extend Thread. If i put this line
"thread.currentThread().sleep(1000);" into the "public void run()",
after i call interrupt(), the thread will stop.

thanks
from Peter ([email protected])
 
T

Tomy

Peter said:
Hi
I have a class that extend Thread. If i put this line
"thread.currentThread().sleep(1000);" into the "public void run()",
after i call interrupt(), the thread will stop.

thanks
from Peter ([email protected])


Did you catch InterruptedException which is being thrown if you
call interrupt on Thread while in sleep().....?????
 
P

Peter

Hi

I have catch InterruptedException , but the thread still running even
you call "interrupt()"

from Peter
(e-mail address removed)
 
P

Peter

Hi

Nothing want to ask, just want to share my experimence.

from Peter
(e-mail address removed)
 
P

Peter

This is my Thread template, even call sleep() in run(), it will stop
when you call interrupt()..

class MyThread extends Thread {
private volatile boolean shouldRun = false;

public void start() {
shouldRun = true;
new Thread(this).start();
}

public void suspendThread() {
shouldRun = false;
}

public boolean isRunning() {
return shouldRun;
}

public void run() {
Thread thisThread = Thread.currentThread();
while (shouldRun) {
try {
sleep(50);
} catch (InterruptedException e) {
}
}
}
}

thanks
from Peter ([email protected])
 
S

Sergio Juan

Peter said:
This is my Thread template, even call sleep() in run(), it will stop
when you call interrupt()..

class MyThread extends Thread {
private volatile boolean shouldRun = false;

public void start() {
shouldRun = true;
new Thread(this).start();

???
It should not even compile IMHO, there is no constructor Thread(Thread) in
java.lang.Thread.
I don't understand this.

Regards.
 
S

Steve Horsley

Hi
I have a class that extend Thread. If i put this line
"thread.currentThread().sleep(1000);" into the "public void run()",
after i call interrupt(), the thread will stop.

thanks
from Peter ([email protected])

Do you have a question? If so, I suggest you post some code with it so
others can see more clearly what you're on about.

Steve
 
S

Steve Horsley

This is my Thread template, even call sleep() in run(), it will stop
when you call interrupt()..

class MyThread extends Thread {
private volatile boolean shouldRun = false;

public void start() {
shouldRun = true;
new Thread(this).start();
}
}
public void suspendThread() {
shouldRun = false;
}
}
public boolean isRunning() {
return shouldRun;
}
}
public void run() {
Thread thisThread = Thread.currentThread();
while (shouldRun) {
try {
sleep(50);
} catch (InterruptedException e) {
}
}
}
}
}
thanks
from Peter ([email protected])

Sorry to be rude, but this is horrible.

MyThread extends Thread but also overrides start() such that instead of
starting the MyThread thread when start() is called, it creates and starts
a different Thread instead.

I don't know how you are excercising this MyThread class, but I suspect
that your apparent problem derives form confusion over which of the two
threads is running.

I suggest that in order to avoid confusion, you do NOT extend Thread at
all, but instead use normal classes that implement Runnable, and create
ordinary Threads as required.

Steve
 
S

Steve Horsley

???
It should not even compile IMHO, there is no constructor Thread(Thread) in
java.lang.Thread.
I don't understand this.

Regards.

But Thread implements Runnable and therefore so does MyThread. And there
is a Thread(Runnable) constructor.

Steve
 
S

Steve Horsley

Thanks for the opinions
If implement Runnable, and you call "sleep()" within run(). After
you call interrupt(), then thread won't stop too.

from Peter

It's not clear whether you want the thread to stop or not.
Anyway, here's an example where the thread doesn't stop when interrupted.
Uncomment the line in the loop to make it stop when it is interrupted.

Steve


/** QuickTest.java */
public class QuickTest implements Runnable {

private volatile boolean shouldRun = false;

public static void main(String[] args) {
// Create and start a QuickTest
QuickTest qt = new QuickTest();
Thread t = new Thread(qt);
t.start();

// wait 5 seconds
try {
Thread.sleep(5000);
} catch(InterruptedException ie) {
System.out.println("Main thread interrupted - shouldn't happen");
}

// interrupt the test thread
t.interrupt();

// wait another 5 seconds
try {
Thread.sleep(5000);
} catch(InterruptedException ie) {
System.out.println("Main thread interrupted - shouldn't happen");
}
}

/** Loop forever. */
public void run() {
shouldRun = true;
while(shouldRun) {
System.out.println("Snooze...");
try {
Thread.sleep(1000);
} catch(InterruptedException ie) {
System.out.println("Sleeper loop was interrupted.");
shouldRun = false;
}
}
System.out.println("Sleeper loop terminating");
}



}
//=============================================================
 
S

Steve Horsley

On Sun, 06 Jul 2003 19:05:55 -0700, Peter wrote:


It's not clear whether you want the thread to stop or not.
Anyway, here's an example where the thread doesn't stop when interrupted.
Uncomment the line in the loop to make it stop when it is interrupted.

Oops, I lied.
The posted example _does_ stop. Comment out the line:
shouldRun = false;
to make it carry on even after an interrupt.

Steve
 
D

Dale King

Steve Horsley said:
Do you have a question? If so, I suggest you post some code with it so
others can see more clearly what you're on about.


I'm gathering that his question involves the fact that the thread is not
interrupted at the instant that interrupt is called on it. If that is the
question, then that is the way it is supposed to work.

It will only be interrupted when in a wait, join, sleep, or blocked in
certain I/O operations in the New I/O API. If it is not currently blocked
all that will happen is that the threads interrupted flag is set so that it
will be interrupted when it does do a blocking operation or you can also
query the status of the interrupt flag with the interrupted method (which
also clears the flag).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top