Intro to CyclicBarrier class in Java First we will take an example, after that we will understand immediately what the CyclicBarrier does. Now, say we have three tasks, and there are two officers, each officer will do those three tasks in order: task 1, task 2 and task 3, in each one’s own thread. So…
Introduction to ExecutorService interface ExecutorService is an interface, we create its instance by static factory method. But first we have to import the concurrent package: Here is a small example. Again we use the simple Runnable example: Ok, nothing special here. execute method Next, to create an instance of ExecutorService, we don’t use the new…
Introduction to Callable interface The Callable interface is a functional interface, we can consider it the counterpart of the Runnable interface. We use the Callable when we expect a return value from the thread. In all of the examples before, we almost always use the Runnable interface. And that is because we do not expect…
Java Test for Object Monitor Don’t try to hack or to do something unusual, this is just some basic Java tests for object monitor, to see what we learned until now. Please try to finish these tests without using an IDE. After choosing your options (radio buttons) please press Check button to see the result….
Object monitor in Java multithreading In the previous example, we can manage our two threads (actually we can have a lot more, say 100 threads, and even more. A normal machine can handle thousands of this kind of light-weight threads) to concurrently access a single object. We know there are at least three ways to…
Thread safe in Java As we can see, multi-threading is not easy. Say we work with two threads. What if both our threads access one data item and change that item? Things get messy real fast, as we will see in the next example. We have a Point class. It is very simple, just an…
Java Test for Multithreading Don’t try to hack or to do something unusual, this is just some basic Java tests for multithreading, to see what we learned until now. Please try to finish these tests without using an IDE. After choosing your options (radio buttons) please press Check button to see the result. Note that…
Runnable interface The problem with the Thread class is that a class in Java can only extend a maximum of one other class. So if our MyThread class extends Thread class, it cannot extend any other class. To meet that real demand, Java provides the Runnable interface, the Runnable interface has only one abstract class,…
Introduction to Java Multithreading Multi-threading is the ability for the Java program to run more than one part (called thread in Java) at one time (concurrently). This is different from CPU cores, or CPU threads, the CPU of the machine can have one or more cores, threads. But that’s not related to Java threads. Java…
Java Test for Java NIO2 Don’t try to hack or to do something unusual, this is just some basic Java tests for Java NIO2, to see what we learned until now. Please try to finish these tests without using an IDE. After choosing your options (radio buttons) please press Check button to see the result….