Funda -2
- Which one would be easy to write? synchronization code for 10 threads or 2 threads?
In terms of writing code, both will be of same complexity because synchronization code is independent of a number of threads. Choice of synchronization though depends upon a number of threads because the number of thread present more contention, so you go for advanced synchronization technique e.g. lock stripping, which requires more complex code and expertise. - How do you call wait() method? using if block or loop? Why?
wait() method should always be called in loop because it’s possible that until thread gets CPU to start running again the condition might not hold, so it’s always better to check condition in loop before proceeding. Here is the standard idiom of using wait and notify method in Java: - What is false sharing in the context of multi-threading?
false sharing is one of the well-known performance issues on multi-core systems, where each process has its local cache. false sharing occurs when threads on different processor modify variables that reside on same cache line
False sharing is very hard to detect because the thread may be accessing completely different global variables that happen to be relatively close together in memory. Like many concurrency issues, the primary way to avoid false sharing is careful code review and aligning your data structure with the size of a cache line. - What is busy spin? Why should you use it?
Busy spin is one of the technique to wait for events without releasing CPU. It’s often done to avoid losing data in CPU cached which is lost if the thread is paused and resumed in some other core. So, if you are working on low latency system where your order processing thread currently doesn’t have any order, instead of sleeping or calling wait(), you can just loop and then again check the queue for new messages. It’s only beneficial if you need to wait for a very small amount of time e.g. in micro seconds or nano seconds. LMAX Disrupter framework, a high-performance inter-thread messaging library has a BusySpinWaitStrategy which is based on this concept and uses a busy spin loop for EventProcessors waiting on the barrier. - How do you take thread dump in Java?
You can take a thread dump of Java application in Linux by using kill -3 PID, where PID is the process id of Java process. In Windows, you can press Ctrl + Break. This will instruct JVM to print thread dump in standard out or err and it could go to console or log file depending upon your application configuration. - is Swing thread-safe?
No, Swing is not thread-safe. You cannot update Swing components e.g. JTable, JList or JPanel from any thread, in fact, they must be updated from GUI or AWT thread. That’s why swings provide invokeAndWait() and invokeLater() method to request GUI update from any other threads. This methods put update request in AWT threads queue and can wait till update or return immediately for an asynchronous update. You can also check the detailed answer to learn more. - What is a thread local variable in Java?
Thread-local variables are variables confined to a thread, its like thread’s own copy which is not shared between multiple threads. Java provides a ThreadLocal class to support thread-local variables. It’s one of the many ways to achieve thread-safety. - Write wait-notify code for producer-consumer problem?
Just remember to call wait() and notify() method from synchronized block and test waiting for condition on the loop instead of if block. - Write code for thread-safe Singleton in Java?
When we say thread-safe, which means Singleton should remain singleton even if initialization occurs in the case of multiple threads. Using Java enum as Singleton class is one of the easiest ways to create a thread-safe singleton in Java. - Can you guarantee the garbage collection process?
No, you cannot guarantee the garbage collection, though you can make a request using System.gc() or Runtime.gc() method.
<Previous> | <Next> |