Posts

Showing posts with the label thread interview questions

Top 10 Java wait, notify, Locking and Synchronization Interview Questions

There has been a lot of articles and books written on how to use wait and notify in Java, how they work, when do you need synchronization, and how to write concurrent code in Java, but, unfortunately, I still see many Java programmer struggles to solve even the classic producer-consumer problem. I know, writing correct concurrent code is challenging and it takes a lot of practice, think through ability and experience to get it right, but at the same time, every Java programmer should be familiar with basics of inter-thread communication, synchronization, locking and understand how all the things work together. They should be able to reason the output of program when the same code is executed by more than one thread, at the same time. They should know that compiler can re-order their code to optimize performance and how they can prevent it. Read more �

Difference between a Thread and an Executor in Java

Even though both Thread and Executor, both are used to executed some code in parallel, there are some key differences between them.The main difference between a Thread and an Executor in Java is that later provides a thread pool in Java. Along with several concurrency utilities like CountDownLatch , CyclicBarrier , Semaphore, FutureTask, Callable interface, and Conditions, JDK 5 also introduced built-in thread pool, which provides set of working threads to run your code in parallel. Since creating, starting, and running a thread is a time-consuming and expensive operation, many Java applications create a pool of thread at start-up and leverage that for executing the task in parallel until Java introduced the built-in thread pool. This thread-pool is known as Executor framework which relieved Java application developers from the responsibility of creating and managing threads Read more �

5 Essential difference between Callable and Runnable interface in Java?

The difference between Callable and Runnable is one of the most frequently asked multi-threading and concurrency interview question in Java world. I remember, it was 2007 when I first heard about Callable interface and that too on a telephonic interview. Till then, I was happy using Runnable to implement threads and just started paying attention to Java 1.5, as most of the application by then using Java 1.4. That one interview question encouraged me to learn more about several other useful features introduced in Java 5 concurrency library e.g. CountDownLatch , CyclicBarrier , Semaphore , Atomic variables, and Thread pool. This is one of the reasons I always encourage Java developer to give/take regular interviews, just to update your knowledge. Read more �

Difference between notify and notifyAll in Java - When and How to use

notify vs notifyAll in Java What is the difference between notify and notifyAll method is one of th e tricky Java questions , w hich is easy to answer but once Interviewer asks follow-up questions, you either got confused or not able to provide clear-cut and to the point answers? The main difference between notify and notifyAll is that notify method will only notify o ne Thread a nd notifyAll method will notify all Threads which are waiting on that monitor or lock. By the way, this is something you have been reading in all over places and to be frank, this statement despite being correct is not complete and its very difficult to understand difference between notify vs notifyAll by just reading this statement. Lot of questions comes in mind like Which thread will be notified if I use notify() ? How do I know how many threads are waiting, so that I can use notifyAll() ? How to call notify()? What are these thread waiting for being notified etc? Actually, discussion of notify...

Difference between ExecutorService.submit() and Executor.execute() methods in Java?

What is the difference between Executor.submit() and Executor.execute() method in Java? is one of the good multi-threading questions for experienced Java programmers, mostly asked in Investment Banks like Barclays, Deutsche Bank, or Citibank. A main difference between the submit() and execute() method is that ExecuterService.submit() can return result of computation because it has a return type of Future , but execute() method cannot return anything because it's return type is void . The core interface in Java 1.5's Executor framework is the Executor interface which defines the execute(Runnable task) method, whose primary purpose is to separate the task from its execution. Read more �

Difference between Wait and Sleep, Yield in Java

The difference between wait and sleep or the difference between sleep and yield in Java are one of the popular core Java interview questions and asked on multi-threading interviews . Out of three methods which can be used to pause a thread in Java, sleep() and yield() methods are defined in thread class while wait() is defined in the Object class, which is another interview question. The key difference between wait() and sleep() is that former is used for inter-thread communication while later is used to introduced to pause the current thread for a short duration. This difference is more obvious from the fact that, when a thread calls the wait() method, it releases the monitor or lock it was holding on that object, but when a thread calls the sleep() method, it never releases the monitor even if it is holding. Read more >>

ReentrantLock Example in Java, Difference between synchronized vs ReentrantLock

ReentrantLock in Java is added on java.util.concurrent package in Java 1.5 along with other concurrent utilities like CountDownLatch , Executors and CyclicBarrier . ReentrantLock is one of the most useful addition in Java concurrency package and several of concurrent collection classes from java.util.concurrent package is written using ReentrantLock , including ConcurrentHashMap , see How ConcurrentHashMap works in Java fo r more details. Two key feature of ReentrantLock, which provides more control on lock acquisition is trying to get a lock with ability to interrupt, and a timeout on waiting for lock, these are key for writing responsive and scalable systems in Java. In short, ReentrantLock extends functionality of synchronized keyword in Java and open path for more controlled locking in Java. In this Java concurrency tutorial we will learn : What is ReentrantLock in Java ? Difference between ReentrantLock and synchronized keyword in Java? Benefits of using Reentrant lock in J...

How to Join Multiple Threads in Java - Thread Join Example

Join method from Thread class is an important method and used to impose order on execution of multiple Threads. The concept of joining multiple threads is very popular on a mutithreading interview question . Here is one of such question, �You have three threads T1, T2, and T3, How do you ensure that they finish in order T1, T2, T3 ?. This question illustrates power of join method on multithreaded programming. Unlike classical thread questions li ke difference between wait and sleep method or solving producer consumer problem in Java , This o ne is a bit tricky. You can do this by using join method, by calling T1.join() from T2 and T2.join() from T3. In this case thread, T1 will finish first, followed by T2 and T3. In this Java multithreading tutorial, we will have a closer look on join method with a simple example. Idea is to illustrate how join method works in simple words. By the way from Java 5 onwards you can also use CountDownLatch and CyclicBarrier cla sses to implement scena...