Java Interview Questions You Need to Know

  1. Is it possible to make array volatile in Java?Answer: Yes, it is possible to make an array volatile in Java, but only the reference which is pointing to an array, not the whole array. Therefore, if one thread changes the reference variable points to another array, which will provide a volatile guarantee.However, if several threads are altering particular array elements, there won’t be any happens before assurance provided by the volatile modifier for such modification.If the purpose is to provide memory visibility guarantee for individual indices of the array, volatile is of no practical use for you. Instead, you must rely on an alternative mechanism for thread-safety in that particular case, e.g. use of a synchronised keyword.From the two, which would be easier to write: synchronisation code for ten threads or two threads?Answer: Both will have the same level of complexity regarding writing the code because synchronisation is independent of the number of threads, although the choice of synchronisation could be subject to the number of threads because this presents more conflict.Therefore, you would opt for an advanced synchronisation technique, e.g. lock stripping, which requires more intricate code and proficiency.How would you call wait() method? Would you use if block or loop, and why?Answer: wait() method should always be called in loop. It is likely that, until the thread gets CPU to start running again, the condition may not hold. Therefore, it is always advised to check the condition in loop before continuing.What is defined as false sharing in the context of multithreading?Answer: False sharing is known to be one of the familiar performance issues on multi-core systems, whereby each process has a local cache.False sharing can be hard to identify since the thread may be retrieving completely different global variables that occur to be fairly close together in memory.Similar to many other concurrency issues, the main way to avoid false sharing is to carefully review your code and supporting your data structure with the size of a cache line.What is busy spin, and why should you use it?Answer: Busy spin is known as one of the techniques to wait for events without freeing CPU. This is often done to avoid losing data in CPU cache, which could get lost if the thread is paused and resumed in some other core.As a result, if you are working on a low latency system where your order processing thread isn’t in any particular order, rather than sleeping or calling wait(), you can just loop and then review the queue for new messages.This is only valuable if you need to wait for a short amount of time, e.g. in microseconds or nanoseconds. LMAX Disruptor frameworks, a high-performance inter-thread messaging library has a BusySpinWait Strategy, which is centred on this model and uses a busy spin loop for EventProcessors waiting on the barrier.LMAX Disruptor frameworks, a high-performance inter-thread messaging library has a BusySpinWait Strategy, which is centred on this model and uses a busy spin loop for EventProcessors waiting on the barrier.How do you take thread dump in Java?Answer: By using kill -3 PID in Linux, where PID is the process id of Java process, you can take a thread dump of Java application. In Windows, you can press Ctrl + Break.This will instruct JVM to print thread dump in standard out, and it may go to console or log file depending on your application configuration.Is Swing thread-safe?AnswerNo, Swing is not thread-safe. You aren’t able to update Swing components, e.g. JTable, JList or Jpanel from any thread. In fact, they must be updated from a GUI or AWT thread. This is why Swing’s provideIn fact, they must be updated from a GUI or AWT thread. This is why Swing’s provide invokeAndWait() and invokeLater() method to request GUI update from alternative threads.These methods put update requests in AWT threads queue and wait for the update or return straight away for an asynchronous update.Describe what a thread-local variable is in JavaAnswer: Thread-local variables are variables restricted to a thread. It is like thread’s own copy which is not shared between a multitude of threads. Java offers a ThreadLocal class to upkeep thread-local variables. This is one of the many ways to guarantee thread-safety.However, it is important to be mindful while using a thread local variable in a controlled environment, e.g. with web servers where worker thread outlives any application variable.Any thread local variable which is not taken away once its work is done can hypothetically cause a memory leak in Java application.What is the difference between sleep and wait in Java?Answer: Both are used to pause thread that is currently running, however, sleep() is meant for short pause because it does not release lock, while wait() is meant for conditional wait.This is why it releases lock, which can then be developed by a different thread to alter the condition of which it is waiting.What is defined as an immutable object? How would you create an immutable object in Java?Answer: Immutable objects are defined as those whose state cannot be changed once it has been made, any alteration will result in a new object, e.g. String, Integer, and other wrapper class.

Leave a Reply