How do you create a Thread? Which approach is recommended? Does Thread class implements Runnable? What is the signature of Thread run method? Define synchronization. Can a thread be interrupted when it is in sleep? Does sleep method throws any Exception? Can the start method be called twice on the same Thread? OR Can the start method invoked again on the same thread object after start been called first time?
Why do we call Thread. Consumer Producer problem. Difference between Thread. How do you interrupt an thread that does not call any method that throws InterruptedException? Interrupts An example for join long milliseconds. Explain the interrupt mechanism. Can run method throw exception? Difference between sleep and wait method in Java. What is thread starvation? What is livelock in multithreading? Explain race condition in multithreading. What causes starvation in threads?
Difference between deadlock and livelock in Java multithreading. What is Slipped Condition in multithreading? What is Intrinsic Lock in Java multithreading? Explain Guarded Blocks in Java. Different states of a Java thread. Difference between synchronizing a static method and a non static method in Java. What is reentrant synchronization in Java?
What is Reentrant lock in Java? Can a thread hold more than one lock at the same time? Difference between synchronized method and synchronized block in Java. What is Runnable in Java? Difference between start and run methods in Java Thread. Define Critical section in Java multi-threading. What is Spurious Wakeups in Java threads? What is Daemon thread in Java? Difference between Daemon and Non Daemon thread in Java.
Difference between a wait and sleep in Threads. Difference between concurrency and parallelism. Explain wait , notify and notifyAll methods in Java threading. Can a thread wait on multiple objects in Java? What is mutex in Java? Explain semaphore in Java. Difference between Mutex and Semaphore in Java. It works by giving a monitor or lock to a particular thread. In other words, a thread when enters in a synchronized method or block, a unique lock gets assigned to it.
Carrying lock signals other threads which also want to access that resource, to wait until the first thread completes its task and release its lock. The lock concept enables us to achieve synchronous behaviour. However, the cons of this concept is that, it increases the waiting time of thread and the total execution time takes much longer than expected.
And even sometimes it raises the situation of Deadlock. A thread that comes first would take the lock and perform its time, meanwhile other thread would wait for first thread to complete its task. However, it follows the same concept as a synchronized method but it has limited scope as compared to the method. The block itself is followed with an argument wrapping reference of an object. A synchronized method locks the monitor associated with the instance of the class ie 'this' or the class if a static method and prevents others from doing so until the return from the method.
A synchronized block can lock any monitor you tell it which and can have a scope smaller than that of the encolsing method. That said, Lock s may be more useful for more complicated things where you can't acquire and release in such a clean manner. I would honestly prefer to avoid using bare Lock s in the first place, and just go with a more sophisticated concurrency control such as a CyclicBarrier or a LinkedBlockingQueue , if they meet your needs.
I've never had a reason to use wait or notify but there may be some good ones. The two different methods are functionally equivalent. There may be a very small performance difference:. At the bytecode level, the synchronized method advertises its need for synchronization as a bit set in the method's access flag.
The JVM looks for this bit flag and synchronizes appropriately. The synchronized block implements its synchronization through a sequence of bytecode operations stored in the class file's definition of the method. So the synchronized method might potentially execute slightly faster and take up less space in terms of bytecode. I'm guessing that the performance difference is negligible and code style guidelines should win out.
Some compilers might even optimize away the block into an access flag.
0コメント