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.
Does each thread has its own thread stack?
Yes each thread has its own call stack.
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.
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.
Which thread related methods are available in Object class?
The methods are:
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.
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.
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.
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.
How to create an immutable class?
To create an immutable class following steps should be followed:
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