How to stop a thread among several threads created with a singleRunnable ?

R

Richard

Hello,

I have several threads created with a single Runnable :

Runnable runnable = new ... ;
for (int i = 0; i < t.length; i++) {
t = new Thread(runnable);
t.start();
}

How can I stop one of these threads (for example t[3].stop()) without
stopping the other ones, and without using stop which is deprecated ?

The example given by Sun to replace stop uses a variable but it does not
work here because all the threads share the same Runnable.

Thanks in advance for your answers.

Richard
 
X

xarax

Richard said:
Hello,

I have several threads created with a single Runnable :

Runnable runnable = new ... ;
for (int i = 0; i < t.length; i++) {
t = new Thread(runnable);
t.start();
}

How can I stop one of these threads (for example t[3].stop()) without
stopping the other ones, and without using stop which is deprecated ?

The example given by Sun to replace stop uses a variable but it does not
work here because all the threads share the same Runnable.

Thanks in advance for your answers.

Richard


Using the same Runnable instance for multiple
threads is a design error. Rethink your design.
 
L

Liz

xarax said:
Hello,

I have several threads created with a single Runnable :

Runnable runnable = new ... ;
for (int i = 0; i < t.length; i++) {
t = new Thread(runnable);
t.start();
}

How can I stop one of these threads (for example t[3].stop()) without
stopping the other ones, and without using stop which is deprecated ?

The example given by Sun to replace stop uses a variable but it does not
work here because all the threads share the same Runnable.

Thanks in advance for your answers.

Richard


Using the same Runnable instance for multiple
threads is a design error. Rethink your design.

and DONT use stop()
set a flag that the thread looks at
 
M

Mark A. Washburn

Richard said:
Hello,

I have several threads created with a single Runnable :

Runnable runnable = new ... ;
for (int i = 0; i < t.length; i++) {
t = new Thread(runnable);
t.start();
}

How can I stop one of these threads (for example t[3].stop()) without
stopping the other ones, and without using stop which is deprecated ?

The example given by Sun to replace stop uses a variable but it does not
work here because all the threads share the same Runnable.

Thanks in advance for your answers.

Richard


The proper way maybe to use a tagged protocol. Where a message is
sent to tasks which are polling for messages. Subclass the Runnable
class
with an instance that has synchronized get/put methods to/from a
variable.
( for example, a boolean called runEnabled )

Otherwise for multiple parallel process design scalability, consider a
Communicating Sequential Processes ( CSP) paradigm such as explained
with Kent University's JCSP.

maw
 
R

Richard

xarax said:
Richard said:
Hello,

I have several threads created with a single Runnable :

Runnable runnable = new ... ;
for (int i = 0; i < t.length; i++) {
t = new Thread(runnable);
t.start();
}

How can I stop one of these threads (for example t[3].stop()) without
stopping the other ones, and without using stop which is deprecated ?

The example given by Sun to replace stop uses a variable but it does not
work here because all the threads share the same Runnable.

Thanks in advance for your answers.

Richard



Using the same Runnable instance for multiple
threads is a design error. Rethink your design.


But in some conditions, it is an easy way to share data between several
threads. Anyway, I was wondering if there is a simple solution to my
problem. Can you tell me one?
 
T

Thomas Schodt

Richard said:
But in some conditions, it is an easy way to share data between several
threads. Anyway, I was wondering if there is a simple solution to my
problem. Can you tell me one?

An instance per thread, static (class) variables for shared data.
 
J

Joona I Palaste

Richard said:
xarax said:
Richard said:
Hello,

I have several threads created with a single Runnable :

Runnable runnable = new ... ;
for (int i = 0; i < t.length; i++) {
t = new Thread(runnable);
t.start();
}

How can I stop one of these threads (for example t[3].stop()) without
stopping the other ones, and without using stop which is deprecated ?

The example given by Sun to replace stop uses a variable but it does not
work here because all the threads share the same Runnable.

Thanks in advance for your answers.


Using the same Runnable instance for multiple
threads is a design error. Rethink your design.

But in some conditions, it is an easy way to share data between several
threads. Anyway, I was wondering if there is a simple solution to my
problem. Can you tell me one?

Use a "container" object that is shared between all threads.

Container cont = new Container();
for (int i=0; i<t.length; i++) {
(t=new Thread(new SharingRunnable(cont))).start();
}

public class SharingRunnable implements Runnable {
public SharingRunnable(Container cont) {
/* ... */
}
public void run() {
/* ... */
}
}

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You have moved your mouse, for these changes to take effect you must shut down
and restart your computer. Do you want to restart your computer now?"
- Karri Kalpio
 
J

Jesper Nordenberg

Richard said:
Hello,

I have several threads created with a single Runnable :

Runnable runnable = new ... ;
for (int i = 0; i < t.length; i++) {
t = new Thread(runnable);
t.start();
}

How can I stop one of these threads (for example t[3].stop()) without
stopping the other ones, and without using stop which is deprecated ?


While I don't recommend you use the same Runnable for all threads,
your problem can be solved using the thread interrupted flag. Here's
an example (Warning: the code is NOT tested!):

new Runnable() {
public void run() {
try {
while (!Thread.interrupted()) {
doSomething(); // Must return after a short period of time
Thread.sleep(100);
}
}
catch (InterruptedException e) {
// Ignore
}
}
}

To stop a thread, do this:

public void stop(int index) {
t[index].interrupt();
t[index] = null;
}

/Jesper Nordenberg
 
M

Michael Borgwardt

Richard said:
But in some conditions, it is an easy way to share data between several
threads. Anyway, I was wondering if there is a simple solution to my
problem. Can you tell me one?

I think it could be done using ThreadLocal
 
W

William Brogden

xarax said:
Hello,

I have several threads created with a single Runnable :

Runnable runnable = new ... ;
for (int i = 0; i < t.length; i++) {
t = new Thread(runnable);
t.start();
}

How can I stop one of these threads (for example t[3].stop()) without
stopping the other ones, and without using stop which is deprecated ?

The example given by Sun to replace stop uses a variable but it does
not
work here because all the threads share the same Runnable.

Thanks in advance for your answers.

Richard



Using the same Runnable instance for multiple
threads is a design error. Rethink your design.


But in some conditions, it is an easy way to share data between several
threads. Anyway, I was wondering if there is a simple solution to my
problem. Can you tell me one?


Give each Thread a different name.
new Thread( runnable, "T" + i );

then use
Thread.currentThread().getName()
to see if the current thread is the one you want to stop.
 
R

Richard

Thanks for all the answers.

I have finally found another solution. This solution uses interrupt()
and a loop controled by ! Thread.interrupted(). I wonder why this
solution is not always used (even when the threads are not waiting)
instead of the solution given by Sun that uses a variable that is set to
null to stop a thread. Someone can explain it to me?

Here is my solution :

public class Waiter3 implements Runnable {

public void run() {
Thread thisThread = Thread.currentThread();
int i = 0;
// Just to see the thread working
System.out.println(thisThread.getName() + i++);
while (! Thread.interrupted()) {
try {
Thread.sleep(1300);
}
catch(InterruptedException e) {
thisThread.interrupt();
}
System.out.println(thisThread.getName() + i++);
}
}
}

class Main3 {
public static void main(String[] args) throws InterruptedException {
Waiter3 waiter = new Waiter3();
Thread t1 = new Thread(waiter, "t1");
Thread t2 = new Thread(waiter, "t2");
t1.start();
t2.start();
// Sleep a little
Thread.sleep(3000);
// Stop thread t1, but not t2
System.out.println("Stop t1");
t1.interrupt();
Thread.sleep(15000);
System.out.println("Stop t2");
t2.interrupt();
}
}

Richard

William said:
xarax said:
Hello,

I have several threads created with a single Runnable :

Runnable runnable = new ... ;
for (int i = 0; i < t.length; i++) {
t = new Thread(runnable);
t.start();
}

How can I stop one of these threads (for example t[3].stop()) without
stopping the other ones, and without using stop which is deprecated ?

The example given by Sun to replace stop uses a variable but it does
not
work here because all the threads share the same Runnable.

Thanks in advance for your answers.

Richard



Using the same Runnable instance for multiple
threads is a design error. Rethink your design.


But in some conditions, it is an easy way to share data between
several threads. Anyway, I was wondering if there is a simple solution
to my problem. Can you tell me one?



Give each Thread a different name.
new Thread( runnable, "T" + i );

then use
Thread.currentThread().getName()
to see if the current thread is the one you want to stop.
 
J

Jesper Nordenberg

Richard said:
Thanks for all the answers.

I have finally found another solution. This solution uses interrupt()
and a loop controled by ! Thread.interrupted(). I wonder why this
solution is not always used (even when the threads are not waiting)
instead of the solution given by Sun that uses a variable that is set to
null to stop a thread. Someone can explain it to me?

Don't know. When was the Thread.isInterrupted() method introduced?
try {
Thread.sleep(1300);
}
catch(InterruptedException e) {
thisThread.interrupt();

The line above is unnecessary. You should just put the catch statement
outside the while loop instead.

/Jesper Nordenberg
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top