This set of Java Assessment Questions and Answers on “isAlive(), Join() & Thread Synchronization”.
1. Which of this method can be used to make the main thread to be executed last among all the threads? Answer: b 2. Which of this method is used to find out that a thread is still running or not? Answer: c 3. What is the default value of priority variable MIN_PRIORITY AND MAX_PRIORITY? Answer: c 4. Which of these method waits for the thread to terminate? Answer: c 5. Which of these method is used to explicitly set the priority of a thread? Answer: c 6. What is synchronization in reference to a thread? Answer: a 7. What will be the output of the following Java code? a) My Thread 8. What will be the output of the following Java code? a) My Thread 9. What will be the output of the following Java code? a) 0 10. What will be the output of the following Java code? a) true
a) stop()
b) sleep()
c) join()
d) call()
Clarification: By calling sleep() within main(), with long enough delay to ensure that all child threads terminate prior to the main thread.
a) run()
b) Alive()
c) isAlive()
d) checkRun()
Clarification: The isAlive( ) method returns true if the thread upon which it is called is still running. It returns false otherwise.
a) 0 & 256
b) 0 & 1
c) 1 & 10
d) 1 & 256
Clarification: None.
a) sleep()
b) isAlive()
c) join()
d) stop()
Clarification: None.
a) set()
b) make()
c) setPriority()
d) makePriority()
Clarification: The default value of priority given to a thread is 5 but we can explicitly change that value between the permitted values 1 & 10, this is done by using the method setPriority().
a) It’s a process of handling situations when two or more threads need access to a shared resource
b) It’s a process by which many thread are able to access same shared resource simultaneously
c) It’s a process by which a method is able to access many different threads simultaneously
d) It’s a method that allow too many threads to access any information require
Clarification: 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
class newthread extends Thread
{
newthread()
{
super("My Thread");
start();
}
public void run()
{
System.out.println(this);
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error
Clarification: 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.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[My Thread,5,main].
class newthread extends Thread
{
Thread t;
newthread()
{
t = new Thread(this,"My Thread");
t.start();
}
public void run()
{
try
{
t.join()
System.out.println(t.getName());
}
catch(Exception e)
{
System.out.print("Exception");
}
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
b) Thread[My Thread,5,main]
c) Exception
d) Runtime Error
Clarification: 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 ‘t.join()’ leads to runtime error and nothing will be printed on the screen.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
class newthread extends Thread
{
Thread t;
newthread()
{
t = new Thread(this,"New Thread");
t.start();
}
public void run()
{
System.out.println(t.isAlive());
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
b) 1
c) true
d) false
Clarification: 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.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
true
class newthread extends Thread
{
Thread t1,t2;
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();
}
}
b) false
c) truetrue
d) falsefalse
Clarification: 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.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse