Question
class newthread implements Runnable {
Thread t;
newthread() {
t1 = new Thread(this,"Thread_1");
t2 = new Thread(this,"Thread_2");
t1.start();
t2.start();
}
public void run() {
t2.setPriority(Thread.MAX_PRIORITY);
System.out.print(t1.equals(t2));
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
What is the output of this program?
class newthread implements Runnable {
Thread t;
newthread() {
t1 = new Thread(this,"Thread_1");
t2 = new Thread(this,"Thread_2");
t1.start();
t2.start();
}
public void run() {
t2.setPriority(Thread.MAX_PRIORITY);
System.out.print(t1.equals(t2));
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
Answer: Option D
Was this answer helpful ?
Threads t1 & t2 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 t1 executes
than the run method of t2 printing 2 times “false†as both the threads are not equal one is
having different priority than other, hence falsefalse is printed.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse
Was this answer helpful ?
Submit Solution