0

When i run the following program, one javaw.exe running for all threads in Windows task manager. But want to run each Thread into individual javaw.exe (Runtime). Also javaw.exe should be killed at end of the Thread process. How to do that?

public class Task extends Thread {
    String name;

    public Task(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println("Started: " + name);
        try {
            if (name.equals("A")) {
                sleep(3000);
            } else if (name.equals("B")) {
                sleep(6000);
            } else if (name.equals("C")) {
                sleep(9000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Completed: " + name);
    }

    public static void main(String[] args) {
        new Task("A").start();
        new Task("B").start();
        new Task("C").start();
    }
DavidPostill
  • 7,734
  • 9
  • 41
  • 60
Manoj
  • 311
  • 2
  • 5
  • 14
  • 1
    When running this pgm, 3 runtime javaw.exe should be run, after 3 sec Thread A runtime should be killed, after 6 sec Thread B runtime should be killed... – Manoj Jul 18 '14 at 06:15
  • 1
    *at end of the Thread process* - is a *very* wrong statement. I suggest you read basics of *threads* and *processes* before trying to code such things. – TheLostMind Jul 18 '14 at 06:17

1 Answers1

2

You can not have a thread be in a different runtime without putting each thread in a different process as that goes against the definition of a thread.

What you want instead is to create multiple processes

Threads by definition take place in a single process. (or a single runtime) Description of differences here: What is the difference between a process and a thread?

The answer is shown here: How to create a process in Java

Community
  • 1
  • 1
dtracers
  • 1,534
  • 3
  • 17
  • 37