Multithreading
- What is the output of this program?
class N extends Thread
{
Thread thread1,thread2;
N()
{
thread1 = new Thread(this,"Thread");
thread2 = new Thread(this,"Thread");
thread1.start();
thread2.start();
}
public void run()
{
thread2.setPriority(Thread.MAX_PRIORITY);
System.out.println(thread2.equals(thread1));
}
}
public class multithreaded_programing
{
public static void main(String args[])
{
new N();
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
This program was previously 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.
- Which of these method of Thread class is used to find out the priority given to a thread?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
getPriority()
- What is the output of the thread in output of this program?
public class Output
{
public static void main(String args[])
{
Thread thread = Thread.currentThread();
System.out.println(thread.isAlive());
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
Thread t is seeded to currently program, hence when you run the program the thread becomes active & code ‘thread.isAlive’ returns true.
- What is the name of the thread in output of this program?
public class multithreading_NameExample
{
public static void main(String args[])
{
Thread thread = Thread.currentThread();
thread.setName("Thread One");
System.out.println(thread.getName());
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
The getName() function is used to obtain the name of the thread, in this code the name given to thread is ‘Thread One’.
- What is the priority of the thread in output of this program?
public class multithreaded_PriorityExample
{
public static void main(String args[])
{
Thread thread = Thread.currentThread();
thread.setName("Thread One");
System.out.println(thread.getPriority());
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
The getPriority() function is used to obtain the priority of the thread, in this code the priority given to thread is ‘5’.