Multithreading
- What is the output of this program?
class N extends Thread
{
Thread thread, thread1, thread2;
N()
{
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 multithreading_Example
{
public static void main(String args[])
{
new N();
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
This program is done by using Runnable interface, here we have used Thread class. This shows both the method are equivalent, we can use any of them to create a thread.
- What is the output of this program?
class N extends Thread
{
Thread thread;
String Str;
N(String ThreadName)
{
Str = ThreadName;
thread = new Thread(this,Str);
thread.start();
}
public void run()
{
}
}
public class multithreading_EqualsExample
{
public static void main(String args[])
{
N object1 = new N("Hello");
N object2 = new N("Java");
try
{
System.out.print(object1.thread.equals(object2.thread));
}
catch(Exception e)
{
System.out.print("Running main thread...");
}
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
Both object1 and object2 have threads with different name that is “Hello” and “Java” hence object1.thread.equals(object2.thread) returns false.
- What is the output of this program?
class N extends Thread
{
Thread thread;
String Str;
N(String ThreadName)
{
Str = ThreadName;
thread = new Thread(this,Str);
thread.start();
}
public void run()
{
}
}
public class multithreading_EqualsExample
{
public static void main(String args[])
{
N object1 = new N("Hello");
N object2 = new N("Java");
try
{
System.out.print(object1.thread.equals(object2.thread));
}
catch(Exception e)
{
System.out.print("Running main thread...");
}
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
Thread.sleep(1000) has caused all the threads to be suspended for some time, hence onject1.thread.isAlive() returns false.
- What is the output of this program?
class N extends Thread
{
Thread thread;
String Str;
N(String ThreadName)
{
Str = ThreadName;
thread = new Thread(this,Str);
thread.start();
}
public void run()
{
}
}
public class multithreading_Example
{
public static void main(String args[])
{
N object1 = new N("First");
N object2 = new N("Second");
try
{
object1.thread.wait();
System.out.print(object1.thread.isAlive());
}
catch(Exception e)
{
System.out.print("Running Main Thread...");
}
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
object1.thread.wait() causes main thread to go out of processing in sleep state hence causes exception and “Main thread interrupted” is printed.
- What is synchronization in reference to a thread?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
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