JAVA - Launching independent Threads

b4u

Golden Member
Nov 8, 2002
1,380
2
81
Hi,

I'm trying to make a program that launches a kind of threads, that are independent of the caller program.

I want to make a program that puts some threads running on the system, then the caller ends but still the threads keep running, and will eventually end/die by themselves, when they finish what they have to do ...

I tried using the Thread class, and it works perfectly, except that the caller can keep running as the threads are launched and running, but still it will only stop when all threads die ... and I want the main program to be some kind of launch-and-finish program ...

Any idea how to make it work? My final objective is to make a kind of TSR java program.


Thanks
 

b4u

Golden Member
Nov 8, 2002
1,380
2
81
But if I make them Daemon threads, when the main program (caller) ends, the Java VM will kill them without warning, leaving their work half way through ... :(
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
When the class with the main() method exits, the jvm terminates. I'm a little unsure of what exactly you're trying to do.

But I'll try a basic diagram: (--- = left thread launches right thread)

Main Thread --- Worker1
Main Thread --- Worker2
Main Thread --- Worker3

So you have 3 threads doing work, and the main "control" thread that launched them still running.

If you want the main thread to wait until all three worker threads are finished before continuing you need:
Worker1.join();
Worker2.join();
Worker3.join();

.join() won't return until that thread is finished, if they finish out of order, it doesn't matter. Once all join()s have returned, you can safely do "System.exit(0);" and terminate the jvm knowing all work has been completed.

If this isn't what you're trying to do, you need to better explain your goals.
 

b4u

Golden Member
Nov 8, 2002
1,380
2
81
For example:

Open task manager, go to processes, and look through the list ... you should have (everyone has) lot's of processes running, some of them without any interface, no opened window, nothing!

That's what I want to do, put something to run on my system without any interface at all ... like a program to monitor a specific directory for files, and when any file arrives, it does some processing. Something similar to services in windows.

If I run a java program, I'll end up with a command window, that closes when the program finishes, and I want to avoid it.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Just make your program and run it using javaw instead of java.

examp:

javaw myprogram

You should then be able to close the window
 

b4u

Golden Member
Nov 8, 2002
1,380
2
81
Originally posted by: amdfanboy
Just make your program and run it using javaw instead of java.

examp:

javaw myprogram

You should then be able to close the window

It seems it works as I want in windows ... is javaw available in other systems, like Linux? Will it work the same way?
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Originally posted by: b4u
Originally posted by: amdfanboy
Just make your program and run it using javaw instead of java.

examp:

javaw myprogram

You should then be able to close the window

It seems it works as I want in windows ... is javaw available in other systems, like Linux? Will it work the same way?


javaw is only available on windows.