robert arles robert.arles.us

robert.arles.us

Github Twitter?
about1 android1 backlight1 bash1 bayesian probability1 bayesian probablilty1 browsermob2 django5 docker1 firstpost1 flask1 go2 golang2 hugo1 init1 java1 jenkins2 jinja1 keyboard1 linux1 lubuntu1 mitmproxy1 nerdlynews1 nginx1 nltk1 pageloadstats2 pelican1 python11 qa automation1 rabot2 saucelabs1 selenium4 threading1 ubuntu2 webdriver4

Threading

Java Threading, a very simple introduction.

Threading in Java is really fairly simple. At first, it really might not seem that way, but there aren’t a lot of parts to a simple multi-threaded model. The difficulty only comes at later, more complex, stages of application development, such as controlling multiple threads sharing data and resources. That problem is very similar to having one person vs a crowd of people sharing food and living space. With one person, there is no real extra effort required, you can just about let them do as they please. When sharing is required, conflicts will arise, and people get difficult to manage. Don’t they?

So lets stay simple, and gain some clarity and a nice simple foundation on the topic.

In a simple model, there are two major components to be concerned with:

  • A calling class with a public static void main() method that will create and start threads. No structure required beyond that of any simple Java app, like the ubiquitous “Hello World!” example.
  • A Thread class. This class extends Thread and overrides the run() method. It probably also needs a initializer that sets any passed in variables. Sticking with the idea of a simple “Hello World!” app, this thread class is where you’d put that System.out.println("Hello, World!"); As a matter of fact, why don’t we just write a threaded version of that as an example?

First, lets create the part of the app that you can think of as the thread itself. It’s the part that, once you start it running, it’s off and running all as a thread of it’s own. Well, not entirely on it’s own, but don’t worry about that, yet. That’s a bit more of an advanced topic and we can stay in a little “threads are magical and run off by themselves” world-view. At least for now. So create an OutputThread.java class file and add the following:

// Note that the class extends Thread
public class OutputThread extends Thread {
    private String myMessage;
    OutputThread(String message){
        System.out.println("Creating a thread");
        myMessage = message;
    }

    public void run(){
        System.out.println(myMessage);
    }

}

The first line creates a class, of course, but also tells java that you want this class to extend the Thread class. The Thread class is a template that defines your new class as capable of being used for threading. When you extend Thread, you need to define at least one method, run(). The run() method is similar to the main() method in a Java app. For this example, we’re just printing our “Hello” message there, but it’s where you’d define all of the super-fancy work you want your thread to run off and do, just like you’d do in a main() method for an non-threaded app. The constructor is required ONLY because we are passing in a String to print, so we set a variable myMessage here. Because you extended Thread, you get a start() method for free! It’s free, so use it. Really. Remember this. Use start() and only start(). This is critical. The start method is what is used to launch your thread. Anything you create in a thread class might run when called, but that WILL NOT actually launch your thread, it will fool you and just run the method.

We’re almost done!

Next, create the main() part of our threaded app. Create a HelloThreadedWorld.java class file and put this in it:

public class HelloThreadedWorld {

    public static void main(String[] args){
        OutputThread outputThread = new OutputThread("Hello, Threaded World!");
        outputThread.start();
    }

}

So in the above code, you simply create an instance of your OutputThread class, then start() it. Note that we pass our “Hello” message to the thread, but the thread will do the printing. If you run this, you should get output similar to this:

Creating a thread
Hello, Threaded World!

This is so simple that you may actually not understand the benefit of using a thread. I understand. In the next article, I’ll take this a bit further and demonstrate that this is really, honestly creating a thread that is running on it’s own, separate from the control of the main() method.

-