...
When a class is defined within a scope of another class, then it becomes inner class. If the access modifier of the inner class is static, then it becomes nested class.
Concurrency - object vs class locking
At the heart of concurrency, there lie the concepts of object locking. Locking happens at instance level as well as class level.
- Object level locking is mechanism when you want to synchronize a non-static method or non-static code block such that only one thread will be able to execute the code block on given instance of the class. This should always be done to make instance level data thread safe.
- Class level locking prevents multiple threads to enter in a synchronized block in any of all available instances on runtime. This means if in runtime there are 100 instances of
DemoClass
, then only one thread will be able to executedemoMethod()
in any one of the instances at a time, and all other instances will be locked for other threads. This should always be done to make static data thread safe.
2.4. Compare and Swap [CAS] Algorithm
This question is targeted towards mid-level or senior developers. This requires a deep understanding of other concurrent concepts before answering this question. So It is a good way to test deep knowledge in Java concurrency.
What is optimistic and pessimistic locking?
What is compare and swap algorithm?
What is an atomic operation?
How AtomicInteger and AtomicLong works?
2.5. What is Fork/Join framework?
This is not a new concept but is now used in multiple ways since the release of Java 8. Fork-Join breaks the task at hand into mini-tasks until the mini-task is simple enough that it can be solved without further breakups. It’s like a divide-and-conquer algorithm. One important concept to note in this framework is that ideally no worker thread is idle. They implement a work-stealing algorithm in that idle workers steal the work from those workers who are busy.
2.6. What is ThreadPoolExecutor?
In concurrent Java application, creating a thread is an expensive operation. And if you start creating new thread instance everytime to execute a task, application performance will degrade surely. ThreadPoolExecutor solves this problem.
ThreadPoolExecutor separates the task creation and its execution. With ThreadPoolExecutor, you only have to implement the Runnable objects and send them to the executor. It is responsible for their execution, instantiation, and running with necessary threads.
Read how ThreadPoolExecutor solves various problems and how it is used with BlockingQueue.
2.8. How to write a deadlock and resolve in Java
It can come in form of a puzzle. Better be ready for it. The interviewer may test your concurrency knowledge and your deep understanding on wait() and notify() method calls.
Be ready with one deadlock source-code example in your finger-tips. You will need it.
Q9. What is the difference between equals() and == in Java?
...