Home » Core Java » threads

threads

Java Thread Join Example

In this article, I am going to show you an example of Thread.join. It can be used in a scenario where the calling thread has to rely on the results of execution of one or more sub-tasks and can’t proceed unless the threads executing these sub-tasks are done with their job. In brief, Thread.join() provides you with wait & notification mechanism where ...

Read More »

Thread.UncaughtExceptionHandler Example

In this example we will see how to use Thread.UncaughtExceptionHandler. UncaughtExceptionHandler is used as a way to provide an elegant way to handle Runtime Exceptions which are not handled otherwise in the programs. As soon as a thread terminates due to an uncaught exception,JVM will query the thread for its UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler() and will invoke the handler’s uncaughtException method, ...

Read More »

Java Daemon Thread Example

In this example we shall show you how to make use Java Daemon Thread, A Thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads (User, Daemon) of execution running concurrently. A Daemon Thread is typically used to provide a general service in the background as long as the program is running ...

Read More »

Dinning Philosophers deadlock example

This is an example of the Dining Philosophers’ problem. A short description of the problem shows that there are N philosphers sitting around a circular table eating and discussing philosphy. The problem is that each philosopher needs 2 forks to eat, and there are only N forks, one between each 2 philosophers. An algorithm is needed for the philosophers to ...

Read More »

Application exits when all daemon threads exit

In this example we shall show you how to create a daemon thread in order to force an application to exit. We have created a class, MyDaemonThread that extends the Thread and overrides its run() method. In short: In its run() method the thread tests if it is a daemon thread, with isDaemon() API method of Thread and sleeps for one ...

Read More »

Daemon Thread example

With this example we are going to demonstrate how to create a daemon Thread. We have implemented a class, MyDaemonThread, that implements the Runnable, as described below: The runnable creates a new Thread, marks it as a daemon, using setDaemon(boolean on) API method of Thread, and then begins the thread’s execution calling its start() API method. The class overrides the run() ...

Read More »

CyclicBarrier example

This is an example of how to use a CyclicBarrier. A CyclicBarrier is a synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it ...

Read More »

CountDownLatch example

In this example we shall show you how to use a CountDownLatch. The CountDownLatch is a synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the countDown() ...

Read More »

Multiple access example

With this example we are going to demonstrate how to have multiple access over an object. The example describes the transfer of amounts of bank accounts in a bank. In short: We have created a class named SwissBank. It has a double array called accounts, a Lock called bankLock and a Condition called sufficientFunds. It has a constructor in which for ...

Read More »

Return a value from a thread

This is an example of how to return the value of a Thread. The steps of the example are described in short: We have implemented two classes, RetDouble and RetInt that both implement the Callable, the first using as parameter a Double and the second one using as parameter an Integer. They both override the call() method of the Callable, and ...

Read More »