Java Interview Questions | Page 4

Question 31:

If code running is a thread creates a new thread what will be the initial priority of the newly created thread?

When a code running in a thread creates a new thread object, the priority of the new thread is set equal to the priority of the thread which has created it.

Question 32:

Does each thread has its own thread stack?


Yes each thread has its own call stack.

For example:-
                 Thread threadDemo1 = new Thread();
                 Thread threadDemo2 = new Thread();
                 Thread threadDemo3 = threadDemo1;
In the above example threadDemo1 and threadDemo3 will have the same stack and threadDemo2 will have its own independent stack.

Question 33:

What happens if a start method is not invoked and the run method is directly invoked?

If a thread has been instantiated but not started its is said to be in new state. Unless until a start() method is invoked on the instance of the thread, it will not said to be alive. If you do not call a start() method on the newly created thread instance thread is not considered to be alive. If the start() method is not invoked and the run() method is directly called on the Thread instance, the code inside the run() method will not run in a separate new thread but it will start running in the existing thread.

Question 34:

Which thread related methods are available in Object class?


The methods are:

  • public final void wait() throws Interrupted exception
  • public final void notify()
  • public final void notifyAll()
Question 35:

Why wait, notify and notifyAll is defined in Object Class and not on Thread class in Java?

In the Java language, you wait() on a particular instance of an Object - a monitor assigned to that object. If you want to send a signal to a single thread that is waiting on that specific object instance then you call notify() on that object. If you want to send a signal to all threads that are waiting on that object instance, you use notifyAll() on that object.

If wait() and notify() were on the Thread instead then each thread would have to know the status of every other thread. How would thread1 know that thread2 was waiting for access to a particular resource? If thread1 needed to call thread2.notify() it would have to somehow find out that thread2 was waiting. There would need to be some mechanism for threads to register the resources or actions that they need so others could signal them when resource was ready or available.

In Java, the object itself is the entity that is shared between threads which allows them to communicate with each other. The threads have no specific knowledge of each other and they can run asynchronously. They run and they lock, wait, and notify on the object that they want to get access to. They have no knowledge of other threads and don't need to know their status. They don't need to know that it is thread2 which is waiting for the resource; they just notify on the resource and whomever it is that is waiting (if anyone) will be notified.

Note:- Java is based on Hoare's monitors idea. In Java all object has a monitor. Threads waits on monitors so, to perform a wait, we need 2 parameters:
- a Thread
- a monitor (any object)

In the Java design, the thread can not be specified, it is always the current thread running the code. However, we can specify the monitor (which is the object we call wait on). This is a good design, because if we could make any other thread to wait on a desired monitor, this would lead to an "intrusion", posing difficulties on designing/programming concurrent programs.

Question 36:

What is a better way of creating multithreaded application, extending Thread class or implementing Runnable?

If a class extends the thread class to have a multithreaded application then this subclass of Thread can not extend any other class and the required application will have to be added to this class as it can not be inherited from any other class. If a class is made to implement Runnable interface, then the class can extend other class or implement other interface.

Question 37:

Once a thread has been started can it be started again?

No. A thread can be started only once in its lifetime. If you try starting a thread which has been already started once an IllegalThreadStateException is thrown, which is a runtime exception. A thread in runnable state or a dead thread can not be restarted.

Question 38:

If a class has a synchronised method and non-synchronised method, can multiple threads execute the non-synchronised methods?

Yes. If a class has a synchronised and non-synchronised methods, multiple threads can access the non-synchronised methods.

Question 39:

How to create an immutable class?

To create an immutable class following steps should be followed:

  • Create a final class.
  • Set the values of properties using constructor only.
  • Make the properties of the class final and private
  • Do not provide any setters for these properties.
  • If the instance fields include references to mutable objects:-
    • Don't allow those objects to be changed.
    • Don't provide methods that modify the mutable objects.
    • Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your  methods.
Question 40:

Which classes in java are immutable?

All wrapper classes in java.lang are immutable –
String, Integer, Boolean, Character, Byte, Short, Long, Float, Double, BigDecimal, BigInteger

Share interview questions


comments powered by Disqus


Social Media