A way to let a thread live after the application has died ?

Hi,

Is there a way to let a thread live after the application that created the thread has died ?

I could not managed to do so using the code below:

// MyThread.java

import java.io.*;

publicclass MyThreadextends Thread

{

public MyThread(){}

publicvoid run()

{

try{

RandomAccessFile rf =

new RandomAccessFile("file.txt","rw");

for(int i = 0; i < 50000; i++)

rf.writeChars(i +" ");

rf.close();

}

catch (Exception e){

System.out.println(e.getMessage());

}

}

}

// RunThread.java

publicclass RunThread

{

public RunThread(){}

publicstaticvoid main(String[] args)throws Exception

{

new MyThread().start();

System.out.println("RunThread is over! (MyThread should continue)");

}

}

Thanks a lot.

[2146 byte] By [kumlali1] at [2007-9-27 18:52:42]
# 1
Make it a daemon . :) Thread t = new MyThread();t.setDaemon( true );t.start();
nishuteddy at 2007-7-6 20:24:39 > top of Java-index,Desktop,Runtime Environment...
# 2

Aaargs. Sudden Impulse. sorry for the earlier post.It should NOT be a daemon.

Anyways ,on my system ( without any modifications) the code works perfectly.

Anyways you could do something like

Thread t = new MyThread();

t.start();

t.join(); // Wait for it to die.

nishuteddy at 2007-7-6 20:24:39 > top of Java-index,Desktop,Runtime Environment...
# 3

Hi nishuteddy,

Thank you for taking your time.

I noticed that my problem is not releated to threads actually.

What I want is to start a program from the consol(command prompt) and let the system to get inputs from its prompt while the application runs. It's like "nohup" in UNIX: start it and continue.

In my situation, after I started the application in consol, the system doesn't allow me to do another thing while waiting for the application to stop.

I think, if I could manage to do so, then I can start the application within my Java application by creating a Runtime process and executing it.

Am I wrong?

Many thanks.

kumlali1 at 2007-7-6 20:24:39 > top of Java-index,Desktop,Runtime Environment...
# 4
You could do a Runtime.getRuntime().exec(..) and it would start a seperate process. You do not then have to wait for new sub Processto die. This might be helpful http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Process.html
nishuteddy at 2007-7-6 20:24:39 > top of Java-index,Desktop,Runtime Environment...
# 5

Well, technically it's impossible!

After all, a thread belongs to a process. If a process dies, all its threads must have died too. If a thread of excecution continues after the process information has been removed from the operating system, then that is a serious bug in the operating system!!

But maybe you mean something else?

You could have a windowing application which didn't kill itself when the main window was closed.

Rhys

RhysP at 2007-7-6 20:24:39 > top of Java-index,Desktop,Runtime Environment...