Tampilkan postingan dengan label Java multithreading Tutorials. Tampilkan semua postingan
Tampilkan postingan dengan label Java multithreading Tutorials. Tampilkan semua postingan

Selasa, 07 Februari 2017

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 �

Jumat, 20 Januari 2017

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. Before answering this question in detail, let's first revise what is a volatile keyword in Java and what kind of guarantee it provides in the context of multithreading and concurrency.
Read more �

Selasa, 20 Desember 2016

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 �

Selasa, 13 Desember 2016

One of my reader Shobhit asked this question on my blog post about 12 must read advanced Java books for intermediate programmers - part1. I really like the question and thought that many Java programmers might have the same doubt whenever someone recommends them to read Java concurrency in Practice. When this book came first in 2006, Java world was still not sure of about new concurrency changes made in Java 1.5, I think the first big attempt to improve Java's built-in support for multi-threading and concurrency. Many Java programmers were even not aware of new tools introduced in the API e.g. CountDownLatch, CyclicBarrier, ConcurrentHashMap and much more. The book offered them the seamless introduction of those tools and how they can use them to write high-performance concurrent Java applications.
Read more �

Kamis, 01 Desember 2016

Java 5 added Executor Framework to provide 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.
Read more �

Sabtu, 20 Agustus 2016

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 �

Rabu, 15 Juni 2016

Books are very important to learn something new and despite being in the electronic age, where books have lost some shine to internet and blogs, I still read and recommend them to get a complete and authoritative knowledge on any topic e.g. concurrent programming. In this article, I will share five best books to learn concurrent programming in Java. These books cover basics, starting from how to create and start a thread, parallel programming, concurrency design patterns, an advantage of concurrency and of course pitfalls, issues, and problems introduced due to multithreading. learning concurrent programming is a difficult task, not even in Java but also on other languages like C++ or modern days JVM languages like Groovy, Scala, Closure, and JRuby.
Read more �

Sabtu, 23 April 2016

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 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 one Thread and 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 and notifyAll is incomplete without discussing wait method in Java and I had touched based on this on my earlier article why to wait and notify must be called from synchronized context. In for order to get answer to those questions and understand difference between notify and notifyAll we will use a simple Java Thread example using wait and notify code :
Read more �

Selasa, 19 April 2016

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 �

Jumat, 25 Maret 2016

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.

Kamis, 07 Maret 2013

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 Javafor 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 Java?
  • Drawbacks of using Reentrant lock in concurrent program?
  • Code Example of ReentrantLock in Java?
Read more �

Rabu, 06 Februari 2013

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 like difference between wait and sleep method or solving producer consumer problem in Java, This one 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 classes to implement scenarios like one thread is waiting for other threads to finish their task.
Read more �