4)
Consider the example and select the correct statement to start the thread.
public class ThreadEx extends Thread
{
public void run()
{
System.out.println("Running...");
}
public static void main(String args[])
{
ThreadEx T1=new ThreadEx();
__________ /*start thread*/
}
}
- ThreadEx.start();
- T1.start();
- ThreadEx.run();
- T1.run();
Correct Answer: 2
T1.start();
start() method starts the execution of thread, when thread execution started Java Virtual Machine calls the run() method.
5)
What will be the output of following program?
class ThreadEx extends Thread
{
public void run()
{
for(int loop=1;loop<=5;loop++)
{
System.out.print(loop);
}
}
public static void main(String args[])
{
ThreadEx T1=new ThreadEx();
ThreadEx T2=new ThreadEx();
T1.start();
T2.start();
}
}
- 1122334455
- 1234512345
- 1122334455... Infinite Times
- 1234512345... Infinite Times
Correct Answer: 1
1122334455
Both threads will be executed simultaneously.