Searching for "thread."

Q:

Explain different ways of creating a thread. Which one would you prefer and why ?

Answer

There are three ways that can be used in order for a Thread to be created:


A class may extend the Thread class.


A class may implement the Runnable interface.


An application can use the Executor framework, in order to create a thread pool.


The Runnable interface is preferred, as it does not require an object to inherit the Thread class. In case your application design requires multiple inheritance, only interfaces can help you. Also, the thread pool is very efficient and can be implemented and used very easily.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

Q:

void waitForSignal() {

Object obj = new Object();

synchronized (Thread.currentThread()) {

obj.wait();

obj.notify();

}

}

Which statement is true?

A) This code can throw an InterruptedException. B) This code can throw an IllegalMonitorStateException.
C) This code can throw a TimeoutException after ten minutes D) All the above
 
Answer & Explanation Answer: B) This code can throw an IllegalMonitorStateException.

Explanation:

It will throw  IllegalMonitorStateException.

Report Error

View Answer Report Error Discuss

Filed Under: Java
Job Role: IT Trainer