Is there anyway to create a new thread..basically create an object of a class within an existing thread and kill this existing thread so only the newly created child remains?

Dani AI

Generated

Short, practical summary for the problem described (and echoed by ): a newly created thread cannot be made to "inherit" the process in the sense that killing its creator magically promotes it — thread lifetimes are independent. Two reliable approaches address the goal of “start a fresh instance and stop the old one”: (1) in-process handover (cooperative shutdown) and (2) out-of-process restart (spawn a new JVM and exit the current one). ’s earlier points about thread lifetime are relevant when choosing between them.

In-process handover (recommended when both instances can share the JVM): run the new instance on its own thread, signal the old instance to stop, and let it clean up. Use a volatile flag or AtomicBoolean plus interruption for blocking ops; prefer ExecutorService for structured control. Example pattern:

class Worker implements Runnable {
    private volatile boolean running = true;
    public void run() {
        while (running) {
            // work...
            try { Thread.sleep(200); } catch (InterruptedException ex) {
                Thread.currentThread().interrupt(); break;
            }
        }
        // cleanup
    }
    public void stop() { running = false; }
}

Start the new Worker, call old.stop(), interrupt if stuck, then join() the old thread (or shutdown()/awaitTermination() an ExecutorService) to ensure resources are released.

Out-of-process restart (when the new instance must outlive the JVM): launch a separate JVM process and then terminate the current one. A simple launcher:

String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
String cp = System.getProperty("java.class.path");
ProcessBuilder pb = new ProcessBuilder(javaBin, "-cp", cp, "com.example.Main");
pb.inheritIO();
Process p = pb.start();
// optional handshake to confirm child ready
System.exit(0);

Cautions: avoid Thread.stop() (unsafe); always close sockets/files and perform cleanup; use interruption for blocking I/O or implement a handshake so the parent exits only after the child is confirmed running. If the JVM lifetime matters, choose daemon vs non-daemon threads deliberately and avoid race conditions by joining with timeouts.

Recommended Answers

All 3 Replies

uth,


You can create a new thread at any time. Then if you so desire, let the 'main' thread die by letting the 'public static main' function return. If the thread you created is a 'daemon' thread the JVM will exit all together. However if the thread you created was a 'user' thread the JVM will not exit straight away, instead another thread will be automatically created called 'DestroyJavaVM' which will wait until all 'user' threads return and then kill the JVM. But why would you want to do this, one thread is as good as any other.


Kate

Hi Kate,

What I need to do is have another instance of the same class running and shut down the existing instance. Will try what you suggested today!Thanks!

uth,


You can create a new thread at any time. Then if you so desire, let the 'main' thread die by letting the 'public static main' function return. If the thread you created is a 'daemon' thread the JVM will exit all together. However if the thread you created was a 'user' thread the JVM will not exit straight away, instead another thread will be automatically created called 'DestroyJavaVM' which will wait until all 'user' threads return and then kill the JVM. But why would you want to do this, one thread is as good as any other.


Kate

hi!!
i wanna ask did u find any solution of below situation. Actually i want to do erxactly the same, kill the exixting thread before creating new thread.

Thx

Hi Kate,

What I need to do is have another instance of the same class running and shut down the existing instance. Will try what you suggested today!Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.