passing parameters to run() method

C

cppaddict

What is the proper way to pass information to new threads?

I am working with some legacy code --- a Runnable object which passes
info to its new threads by setting member variables before starting
them. The run() method uses these member variable values to determine
its behavior.

The problem is that, when many threads are started at once, a member
variable can change before a thread has a chance to execute. When the
thread does execute, it will execute using the wrong parameters
(member variables) because those parameters have changed value in
between the time the thread was started and actually had a chance to
execute. Thus unintended behavior replaces the intended behavior.

My question is: What is the right way to handle a situation like this?
How can you get each new thread its own copy of what are essentially
parameter values?

Thanks for any thoughts,
cpp
 
S

Steve Horsley

cppaddict said:
What is the proper way to pass information to new threads?

I am working with some legacy code --- a Runnable object which passes
info to its new threads by setting member variables before starting
them. The run() method uses these member variable values to determine
its behavior.

This is the normal way to do things.
The problem is that, when many threads are started at once, a member
variable can change before a thread has a chance to execute. When the
thread does execute, it will execute using the wrong parameters
(member variables) because those parameters have changed value in
between the time the thread was started and actually had a chance to
execute. Thus unintended behavior replaces the intended behavior.
Then the design is badly broken. It sounds like you have many threads
running through the same object and fighting over the object's variables.
That's bad news.
My question is: What is the right way to handle a situation like this?
How can you get each new thread its own copy of what are essentially
parameter values?

I would think you should have a different Runnable object for each thread,
each with its own stat. And that any shared info should be in separate
objects with carefully synchronized access.
Thanks for any thoughts,
cpp

HTH
Steve
 
C

Cid

What is the proper way to pass information to new threads?

I am working with some legacy code --- a Runnable object which passes
info to its new threads by setting member variables before starting
them. The run() method uses these member variable values to determine
its behavior.

The problem is that, when many threads are started at once, a member
variable can change before a thread has a chance to execute. When the
thread does execute, it will execute using the wrong parameters
(member variables) because those parameters have changed value in
between the time the thread was started and actually had a chance to
execute. Thus unintended behavior replaces the intended behavior.

My question is: What is the right way to handle a situation like this?
How can you get each new thread its own copy of what are essentially
parameter values?

Thanks for any thoughts,
cpp

Maybe you should consider some kind of Thread factory class.


------ example

....
MyThreadFactory.launchMyThread("param1", 2, new Integer(3) ) ;
....

class MyThread extends Thread {
public void setParam1(String p1) {...}
public void setParam2(int p2) {...}
public void setParam3( Integer p3 ) {...}
}

class MyThreadFactory {
static MyThread launchMyThread(
String param1, int param2, Integer param3
) {...}
}
 
X

xarax

Cid said:
Maybe you should consider some kind of Thread factory class.

Cleaner is to just create a new Runnable for each new Thread,
and stuff the Runnable parameters into its instance fields.
Keep it simple.
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top