Avatar of FranklinRaj22
FranklinRaj22 asked on

spawning thread in a prgram to call a seperate class

I would like to spawn a thread based upon a condition in  my main program, the new thread should run in the back ground doing its work and the main program should countinue running in the foreground in a seperate thread.
Java

Avatar of undefined
Last Comment
Mick Barry

8/22/2022 - Mon
CEHJ

I would use a SwingWorker. We need more detail though
sciuriware

If you got 2 worker jobs I would spawn 2 Threads:

public class Work1 extends Thread
{
....................
}
public class Work2 extends Thread
{
....................


// Main class:

Thread one = new Work1().start();
Thread one = new Work2().start();
...........................
Be careful to always spawn threads if activated by a button,
as you must not 'stay' working in the event thread (actionPerformed() ).

;JOOP!
ASKER
FranklinRaj22

sage,
thanks but i will tel ya my situation, see the pseudocode below , here the main class should countinue its wrk in a seperate thread after it spawns a thread to do the Work1 class.

public class Work1 extends Thread
{
....................
}

//main class
......
new Work1().start();(this would run in the back ground through the entire life time of the application)
.....
.....
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
CEHJ

Is there a GUI involved?
ASKER
FranklinRaj22

yes , the main program which i mentioned would be a SWING GUI, whereas this newly spawned thread would be updating a few file , which my main program might use in alater poitn of time.
CEHJ

Then definitely use a SwingWorker. Do your file manipulation in the doInBackground method
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Mick Barry

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question