Q6) What is use of synchronized keyword?
Ans) synchronized keyword can be applied to static/non-static methods or a block of code. Only one thread at a time can access synchronized methods and if there are multiple threads trying to access the same method then other threads have to wait for the execution of method by one thread. Synchronized keyword provides a lock on the object and thus prevents race condition. E.g.
public void synchronized method(){} public static void synchronized method(){} public void myMethod(){
synchronized (this){ // synchronized keyword on block of code }
}
Q7) What is the difference when the synchronized keyword is applied to a static method or to a non static method?
Ans) When a synch non static method is called a lock is obtained on the object. When a synch static method is called a lock is obtained on the class and not on the object. The lock on the object and the lock on the class don̢۪t interfere with each other. It means, a thread accessing a synch non static method, then the other thread can access the synch static method at the same time but can̢۪t access the synch non static method.
Q8) What is a volatile keyword?
Ans) In general each thread has its own copy of variable, such that one thread is not concerned with the value of same variable in the other thread. But sometime this may not be the case. Consider a scenario in which the count variable is holding the number of times a method is called for a given class irrespective of any thread calling, in this case irrespective of thread access the count has to be increased so the count variable is declared as volatile. The copy of volatile variable is stored in the main memory, so every time a thread access the variable even for reading purpose the local copy is updated each time from the main memory. The volatile variable also have performance issues.
Q9) What is the difference between yield() and sleep()?
Ans) yield() allows the current the thread to release its lock from the object and scheduler gives the lock of the object to the other thread with same priority. sleep() allows the thread to go to sleep state for x milliseconds. When a thread goes into sleep state it doesn’t release the lock.
Q10) What is the difference between wait() and sleep()?
Ans) 1) wait() is a method of Object class. sleep() is a method of Thread class.
2) sleep() allows the thread to go to sleep state for x milliseconds. When a thread goes into sleep state it doesn’t release the lock. wait() allows thread to release the lock and goes to suspended state. The thread is only active when a notify() or notifAll() method is called for the same object.
Q11) What is difference between notify() and notfiyAll()?
Ans) notify( ) wakes up the first thread that called wait( ) on the same object. notifyAll( ) wakes up all the threads that called wait( ) on the same object. The highest priority thread will run first.
Q12) What happens if a start method is not invoked and the run method is directly invoked?
Ans) 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.
Q13) What happens when start() is called?
Ans) A new thread of execution with a new call stack starts. The state of thread changes from new to runnable. When the thread gets chance to execute its target run() method starts to run.
Q14) If code running is a thread creates a new thread what will be the initial priority of the newly created thread?
Ans) 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.
|
Q15) When jvm starts up, which thread will be started up first?
Ans) When jvm starts up the thread executing main method is started.
Q16) What are the daemon threads?
Ans) Daemon thread are service provider threads run in the background,these not used to run the application code generally.When all user threads(non-daemon threads) complete their execution the jvm exit the application whatever may be the state of the daemon threads. Jvm does not wait for the daemon threads to complete their execution if all user threads have completed their execution.
To create Daemon thread set the daemon value of Thread using setDaemon(boolean value) method. By default all the threads created by user are user thread. To check whether a thread is a Daemon thread or a user thread use isDaemon() method.
Example of the Daemon thread is the Garbage Collector run by jvm to reclaim the unused memory by the application. The Garbage collector code runs in a Daemon thread which terminates as all the user threads are done with their execution.
Q17) What all constructors are present in the Thread class?
Ans) Thread() Thread(Runnable target) Thread(Runnable target, String name) Thread(String name)
|
Q18) Can the variables or classes be Synchronized?
Ans) No. Only methods can be synchronized.
|
Q19) How many locks does an object have?
Ans) Each object has only one lock.
|
Q20) Can a class have both Synchronized and non-synchronized methods?
Ans) Yes a class can have both synchronized and non-synchronized methods.
|
|
|
Comments
Post a Comment