Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing args to new thread

Status
Not open for further replies.

stormbind

Technical User
Mar 6, 2003
1,165
GB
Hi,

Can I pass arguments to a new thread when the run() method is called?

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
No.
You don't call run () yourself, but start ().
So you don't have any influence on how run is called.

So can you pass arguments to start ()?
No, because the methodsignature has to be met - else it is a different method.

Is that a problem?
No, because you may pass arguments in your constructor or create a config/setup-method, to pass your arguments.


don't visit my homepage:
 
Yup, go it :)

With an deprecation error that Xlint cannot comment on. I first used new Thread(myCode).start() and that's fine.

It's for an editor that compiles and launches some java in a new window (referred to as the preview operation). Implementing runnable allows me to continue using my editor without closing the preview window :)

However, using the preview a second time results in two preview windows, then three, four, etc. :(

So, I would to exit the preview before launching a new one.

if(preview != null) preview.destroy(); // <-- suspect
preview = new Thread(myCode);
preview.start();

The editor starts. It launches the first preview, and a second preview causes it to hang :(

So... I think destroy() is a problem and javac gave me a deprecated warning that Xlint could not expand on. Any ideas? :)

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Oops! I told a fib.. it causes a long delay, then launches a second preview. Any ideas? :)
 
As join() is not suitable for my scenario, does it mean java cannot stop a thread?

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
You can stop a thread.

Demo 1 (somehow restricted):
Code:
public void run ()
{
   // starts
   // stops by itself
}
// all over now

do it yourself:
Demo 2:
Code:
private volatile boolean condition = true;

public void stopThread ()
{
    condition = false;
}

public void run ()
{
   while (condition) 
   {
       // do your thing
   }
   // stop and over
}

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top