2

I create a thread using

Thread t = new Thread();
t.start();

You start a thread using t.start(); Now how long the thread will be alive? To what state it will go after X (the answer of above question) seconds?

Thread t = new Thread();
t.start();
public void run(){
    System.out.println("Threads");
}

What will happen if the thread has run() method?

Siva
  • 209
  • 3
  • 11
  • 2
    That thread is kind of a bad example because it doesn't execute anything, ie. the body of the `run()` it executes is empty. It will start and end right away. – Sotirios Delimanolis May 23 '14 at 03:24
  • 1
    [**How to search on Google**](https://support.google.com/websearch/answer/134479?hl=en) – Paul Vargas May 23 '14 at 03:25
  • Your thread isn't doing anything, so the answer comes down to scheduling delay. A thread that needs to actually do something will live approximately until its job is done. – user2357112 May 23 '14 at 03:27
  • are you mixing a question on ThreadPools? http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html – Jayan May 23 '14 at 04:55

1 Answers1

2

A thread created and started exactly as you describe will be alive only for as long as the empty Thread.run() method takes to do nothing and return. When the thread terminates, the t.isAlive() function will return false.

Normally, a thread does something useful and will be alive for as long as the run() method has not returned.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • So will the thread state be dead after running the run method? – Siva May 23 '14 at 03:28
  • 1
    If you're specifically asking about the return value of `Thread.getState()`, then it will return `Thread.State.TERMINATED`. Java does not use the word "dead" when referring to thread states. – Greg Hewgill May 23 '14 at 03:33