Archive for category concurrency

Exchanger

Java concurrency API provides a synchronization utility java.util.concurrent.Exchanger that allows two threads to exchange data. Exchanger provides a common point where two threads arrive and exchange data between them such that data of first thread goes to the second thread and data of second thread goes to first. Exchanger basically creates a barrier at which […]

, , ,

Leave a comment

Using local thread variables

Shared data is one of the most critical aspect of concurrent programming. When you create an instance of a class that implements Runnable interface or extends Thread class and use same object to create multiple Threads then all the threads share same attributes. This means if you change any attribute in one thread, all threads […]

, , ,

Leave a comment

Difference between Wait and Sleep in Java

I am listing here differences between wait() and sleep() methods in java. Please feel free to suggest if you can think of anything apart from those listed below. Wait Sleep Wait method release the acquired monitor while the thread waits Sleep method holds the monitor while the thread goes to sleep Wait method should be […]

, ,

Leave a comment

Returning Data from Java Threads

There are a couple of ways in which threads can be created. i.e. Implementing Runnable Interface and extending Thread Class (Example here). Further we need to implement run method and do our processing inside the run method. This would look like below. One thing to note about above snippet is the return type of run() […]

, , ,

6 Comments

Creating and Running thread in Java

Like any other Object in java, Threads are Objects. There are a couple of ways threads can be created in Java. Extending the Thread class and overriding it’s Run() method Building a class that implements Runnable Interface and implementing it’s run() method. Then create an Object of the Thread class and pass an instance of […]

, , ,

1 Comment

UncaughtExceptionHandler in Java

If you do not handle a runtime exception in your program, the default behaviour will be to terminate the program and dump the call stacktrace on the console. Java provides an elegant mechanism of handling Runtime Exceptions that you might have not handled in your program. UncaughtExceptionHandler can be defined at three levels. From highest […]

, , , , , , , ,

Leave a comment