A thread is a single sequential flow of control within a program.
We can implement threading in two ways:
a.Extend the Thread class and override the run() method.
b.Implement the Runnable interface and implement the run() method.
Q2: What is difference between user Thread and daemon Thread?
When we create a Thread in java program, it’s known as user thread. A daemon thread runs in background and doesn’t prevent JVM from terminating. When there are no user threads running, JVM shutdown the program and quits. A child thread created from daemon thread is also a daemon thread.
Q3: What will notify() method do?
notify() method moves a thread out of the waiting pool to ready state, but there is no guaranty which thread will be moved out of the pool.
Q4: Can you notify a particular thread?
Q5: What are different states in lifecycle of Thread?
When we create a Thread in java program, its state is New. Then we start the thread that change it’s state to Runnable. Thread Scheduler is responsible to allocate CPU to threads in Runnable thread pool and change their state to Running. Other Thread states are Waiting, Blocked and Dead.
Q6: What is the difference between sleep() and yield()?
When a Thread calls the sleep() method, it will return to its waiting state. When a Thread calls the yield() method, it returns to the ready state.
Q7: What is a Daemon Thread?
Daemon is a low priority thread which runs in the backgrouund.
Q8: How to make a normal thread as daemon thread?
We should call setDaemon(true) method on the thread object to make a thread as daemon thread.
Q9: What is the difference between normal thread and daemon thread?
Normal threads do mainstream activity, whereas daemon threads are used low priority work. Hence daemon threads are also stopped when there are no normal threads.
Q10: Give one good example of a daemon thread?
Garbage Collector is a low priority daemon thread.
Q11: What does the start() method of Thread do?
The thread's start() method puts the thread in ready state and makes the thread eligible to run. start() method automatically calls the run () method.
Q12: What are the two ways that a code can be synchronised?
a. Method can be declared as synchronised.
b. A block of code be sychronised.
Q13: Can you declare a static method as synchronized?
Yes, we can declare static method as synchronized. But the calling thread should acquire lock on the class that owns the method.
Q14: Can a thread execute another objects run() method?
A thread can execute it's own run() method or another objects run() method.
Q15: What is a deadlock?
A condition that occurs when two processes are waiting for each other to complete before proceeding. The result is that both processes wait endlessly.
Q16: What are all the methods used for Inter Thread communication and what is the class in which these methods are defined?
a. wait(),notify() & notifyall()
wait() method releases CPU, releases objects lock, the thread enters into pool of waiting threads.
notifyAll() method moves all waiting threads from the waiting pool to ready state.
notify() method moves the waiting thread on which it is called from waiting to ready state.
b. Object class
Q17: What is volatile keyword in Java?
When we use volatile keyword with a variable, all the threads read it’s value directly from the memory and don’t cache it. This makes sure that the value read is the same as in the memory.
Q18: Are the static variables saved as the part of serialization?
No. The static variables belong to the class and not to an object they are not the part of the state of the object so they are not saved as the part of serialized object.
Q19: What is use of serialVersionUID?
During object serialization, the default Java serialization mechanism writes the metadata about the object, which includes the class name, field names and types, and superclass. This class definition is stored as a part of the serialized object. This stored metadata enables the deserialization process to reconstitute the objects and map the stream data into the class attributes with the appropriate type.
Everytime an object is serialized the java serialization mechanism automatically computes a hash value. ObjectStreamClass's computeSerialVersionUID() method passes the class name, sorted member names, modifiers, and interfaces to the secure hash algorithm (SHA), which returns a hash value.The serialVersionUID is also called suid.
So when the serilaize object is retrieved , the JVM first evaluates the suid of the serialized class and compares the suid value with the one of the object. If the suid values match then the object is said to be compatible with the class and hence it is de-serialized. If not InvalidClassException exception is thrown.