Disclosure: This article may contain affiliate links. When you purchase, we may earn a small commission.

Grokking The Java Interview - My First Book After 10 Years as Java Blogger

Hello guys, I am very excited to announce the release of my first book after 10 years of writing Java articles and Java interview questions While I have been blogging for the last 10 years, I have never really sold anything, didn’t have any book, course, or any digital product. Finally, now I have my first book and it's going to help Java developer cracking interview — Grokking the Java interview. This was my long desire to have my content in a structured and organized way to provide more value and this book does that. It provides a structured way to prepare for Java interviews and learn essential core Java concepts.

How to write Thread-Safe Code in Java

thread-safety or thread-safe code in Java refers to code which can safely be used or shared in concurrent or multi-threading environment and they will behave as expected. any code, class, or object which can behave differently from its contract on the concurrent environment is not thread-safe. thread-safety is one of the risks introduced by using threads in Java and I have seen java programmers and developers struggling to write thread-safe code or just understanding what is thread-safe code and what is not?

Difference between a Thread and an Executor in Java

Even though both Thread and Executor, both are used to execute some code in parallel, there are some key differences between them. The main difference between a Thread and an Executor in Java is that it 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 a built-in thread pool, which provides a 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 spool of thread at start-up and leverage that for executing the task in parallel until Java introduced the built-in thread pool.

Can You Make an Array or ArrayList Volatile in Java?

This is one of the many interesting multi-threading questions I have shared in my post 50 multi-threading interview questions. Yes, you can make an array volatile in Java, there is no problem with that, neither compiler will flag any error not JVM will throw any exception but the tricky part is why you want to make an array volatile and what is the effect of making an array volatile in Java? In order to answer this question, you must be familiar with both volatile modifier and Java memory model, otherwise, it would be difficult to answer, and that's why it's also one of the trick questions from Java interviews.

Difference between Executor Framework and Fork Join Pool in Java?

Java 5 added Executor Framework to provide an out-of-box thread pool to Java programmers and Java 7 added ForkJoinPool an implementation of ExecutorService which specifically designed to execute ForkJoinTask. The Executor Framework provides several classes e.g. Executor, ExecutorService, and Executors for execution and creating thread pools. It also provides several built-in, ready to use thread pools like a pool of fixed threads, cached thread pool which can expand itself, spawn new threads if required due to heavy load.

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

What is the difference between Executor.submit() and Executor.execute() method in Java? This 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 the result of computation because it has a return type of Future, but the execute() method cannot return anything because'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.

How to Join Multiple Threads in Java? [Thread.join() Example]

Join method from the Thread class is an important method and used to impose order on the execution of multiple Threads. The concept of joining multiple threads is very popular in a multithreading interview question. Here is one 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 the power of the join method on multithreaded programming. Unlike classical thread questions like the difference between the wait and sleep method or solving the producer-consumer problem in Java, This one is a bit tricky.

Top 5 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 questions in the Java world. I remember it was 2007 when I first heard about the 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 applications 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 like CountDownLatch, CyclicBarrier, Semaphore, Atomic variables, and Thread pool. This is one of the reasons I always encourage Java developers to give/take regular interviews, just to update your knowledge.

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 the tricky Java questions, which is easy to answer but once the 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 the notify method will only notify one Thread and the notifyAll method will notify all Threads which are waiting on that monitor or lock. By the way, this is something you have been reading all over places and to be frank,  this statement despite being correct is not complete, and it's very difficult to understand the difference between notify vs notifyAll by just reading this statement.

ThreadLocal in Java - Example Program and Tutorial

ThreadLocal in Java is another way to achieve thread-safety apart from writing immutable classes. If you have been writing multi-threaded or concurrent code in Java then you must be familiar with the cost of synchronization or locking which can greatly affect the Scalability of application, but there is no choice other than synchronizing if you are sharing objects between multiple threads. ThreadLocal in Java is a different way to achieve thread-safety, it doesn't address synchronization requirement, instead, it eliminates sharing by providing an explicit copy of Object to each thread. Since Object is no more shared there is no requirement of Synchronization which can improve scalability and performance of the application.