Examveda
Examveda

What will happen after compiling and running following code?
class A implements Runnable{
      public void run(){
            System.out.println("run-a");
      }
}

public class Test{
      public static void main(String... args){
            A a = new A();
            Thread t = new Thread(a);
            t.start();
            t.start();
      }
}

A. run-a

B. run-a run-a

C. Compilation fails with an error at line 6

D. Compilation succeed but Runtime Exception

E. None of these

Answer: Option D

Solution(By Examveda Team)

Once a thread has been started, it can never be started again. 2nd time t.start() throws java.lang.IllegalThreadStateException.


This Question Belongs to Java Program >> Threads

Join The Discussion

Related Questions on Threads

What is a thread in Java?

A. A lightweight process that runs independently within a program

B. A data structure to store variables

C. A type of loop

D. A synchronization mechanism