Multithreading
- What is the output of this program?
class n extends Thread
{
Thread thread;
n()
{
thread = new Thread(this,"New Thread");
thread.start();
}
public void run()
{
System.out.println(thread.isAlive());
}
}
public class multithreading_Example
{
public static void main(String args[])
{
new n();
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
isAlive() method is used to check whether the thread being called is running or not, here thread is the main() method which is running till the program is terminated hence it returns true.
- What is the output of this program?
class Mythread extends Thread
{
Thread thread;
Mythread()
{
thread = new Thread(this,"New Thread");
thread.start();
}
public void run()
{
try
{
thread.join();
System.out.println(thread.getName());
}
catch(Exception e)
{
System.out.print("Exception");
}
}
}
public class multithreaded_programing
{
public static void main(String args[])
{
new Mythread();
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
join() method of Thread class waits for thread being called to finish or terminate, but here we have no condition which can terminate the thread, hence code ‘thread.join()’ leads to runtime error and nothing will be printed on the screen.
- What is the output of this program?
class Mythread extends Thread
{
Mythread()
{
super("New Thread...");
start();
}
public void run()
{
System.out.println(this);
}
}
public class multithreaded_programing
{
public static void main(String args[])
{
new Mythread();
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Although we have not created any object of thread class still we can make a thread pointing to main method, we can refer it by using this.
- What is synchronization in reference to a thread?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
When two or more threads need to access the same shared resource, they need some way to ensure that the resource will be used by only one thread at a time, the process by which this is achieved is called synchronization
- What is the output of this program?
class newthread implements Runnable
{
Thread thread1,thread2;
newthread()
{
thread1 = new Thread(this,"Thread_1");
thread2 = new Thread(this,"Thread_2");
thread1.start();
thread2.start();
}
public void run()
{
thread2.setPriority(Thread.MAX_PRIORITY);
System.out.println(thread1.equals(thread2));
}
}
public class multithreaded_Example
{
public static void main(String args[])
{
new newthread();
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
Threads thread1 & thread2 are created by class newthread that is implementing runnable interface, hence both the threads are provided their own run() method specifying the actions to be taken. When constructor of newthread class is called first the run() method of thread1 executes than the run method of thread2 printing 2 times “false” as both the threads are not equal one is having different priority than other, hence false false is printed.