Since I’m getting back on programming, I’m currently re-skilling myself on this duty. Having not programmed Java in a while, I’ve found myself on trouble on this simple topic: Java concurrency and threads. I will insert on the blog some “quick tutorials” about various subjects. I’ve found that programmers sometimes need a fast solution on a given problem. They could surely get it by looking at the API’s and so on, but it costs valuable time. So I will go straight to the problem.

  • Problem: I need to do a fixed job on some data in a parallel fashion.
  • Solution: using a multi-threaded model, I have two classes Controller and Worker. Worker is essentially a thread that does the fixed job and reports the data to the Controller
  • Main issues: is it better to “extends Thread” or to” implements Runnable“? How to accessing shared variables concurrently and avoid race conditions? How to pass data from Worker to Controller?

Now let’s talk of the main issues:

  • Thread or Runnable? : Java has 2 main methods to build a thread. You can extend Thread class or you can implements Runnable interface. According to some FAQ answers I’ve found around:

    A Java Thread controls the main path of execution in an application. When you invoke the Java Virtual Machine with the java command, it creates an implicit thread in which to execute the main method. The Thread class provides a mechanism for the first thread to start-up other threads to run in parallel with it.

    The Runnable interface defines a type of class that can be run by a thread. The only method it requires is run, which makes the interface very easy to to fulfil by extending existing classes. A runnable class may have custom constructors and any number of other methods for configuration and manipulation. (from CodeStyle)

    Another detailed explanations of pros & cons can be found at DeveloperLife.com. For our problem, I’ve choosen to implement the Runnable interface, because we don’t need to redefine a new ExtendedThread behaviour, but we only need to redefine the run() method. Moreover, we can also extend from another class, and this will come in handy for the other problems! So, we create our class:

    public class Worker implements Runnable{
    ...
    public final void run(){
    /* here we do the main stuff we need */
    ...
    }
    }

  • Deal with race conditions: in a multithreaded model, we don’t know what will be the effective order of execution. So we need to take care of race conditions, as the threads could potentially compete for the same resources, or even get in a deadlock. In our example, we could think that Controller class declares some variables that will be accessed by threads (for example, a variable where to store the result of the computation from the various threads). To achieve a correct behaviour, we can use synchronized construct to force correct behaviour of our code blocks that mess with the state of our ojbects. We’ll see later how to do this in the code.
  • How to get data from threads: we are doing parallel asynchronous work, we don’t want blocking calls and so on until is absolutely necessary, so a convenient way to pass data from threads to our main class is applying the Observer Pattern. Java provides this behaviour with java.util.Observable class and java.util.Observer interface.

I will eventually complete this with some example code smiley