Multi-threaded programming languages can run two or more tasks at the same time. Those tasks are called threads and in Java you can create them easily.





Ouput's preview

Today we’re going to see how to implement multithreading in a Java program.

Using multithreading

Using multithreading

Without multithreading

Without multithreading

There are two ways we can do it:

  • extend the Thread class
  • implement the Runnable class

Java allows to extend only one class, but you can implement many.

Therefore, if your class already extends a parent class, you have to choose the second option, otherwise you can go for the first.

Extending the Thread class


Objects instanced from the Thread class manage the threads of a program, that are those parts which run in parallel.

As we’ve done for the JFrame class, we can make our class extend Thread, so that it inherits all its methods and variables.

Then we have to override the run method. The code you write in there will be the one executed in a different thread.

The run method doesn’t repeat by itself, so you may want to use a loop.

To start this new thread, create an instance of your class:

and call the start method:

This function will create another thread, in which the run method will be executed.

However, you can’t extend a second class, because Java doesn’t allow that.

If you want to run a JFrame child class as separate thread, you have to use the second option.

Implementing the Runnable class


The other way consists in directly creating a Thread object.

The Thread class’s constructor requires a Runnable object, which contains the code to be executed in the new thread.

threadName is optional.

So, all you have to do is implementing Runnable in your class

overriding the run method and creating a Thread object using an instance of your class:

Now you can start your thread using the start method.

Important functions for threads

In my opinion the 4 most important functions for threads are:

  1. sleep()
  2. yield()
  3. run()
  4. start()

We’ve already seen the last two, but I want to include them anyway to compare a little, but important detail.

sleep and yield are static, so they aren’t called by a specific object and they perform their functions for the thread they’re called in.

On the other hand, run and start depend on the object from which they’re called, not on the current thread.

Remember: you have to call a static function from its class, not from an object. For example, in order to use sleep, you have to write Thread.sleep(ms), not my_thread.sleep(ms).

Method Description
sleep(long millisec) Pauses the current thread for millisec number of milliseconds
yield() Causes the current thread to yield to others thread of the same priority
run() Contains the code to be executed in a new thread
start() Creates a new thread in which it executes run()

There are many other functions concerning threads, but I’m not going to cover them. If you want you can read the list here: Core Java

A program automatically creates a thread when it’s executed (the main thread), so we can still use some of the functions for threads, even if we don’t create any new ones.

Using yield wouldn’t make any sense with only the thread running. Neither start would, because the main thread is already running.

On the other hand we can still use sleep to pause a program.

Conclusion


Creating threads in Java is easy and today you’ve learnt its basic concepts.

There are a lot of detailed sources on the Internet that explain this topic, so there’s no need for me to write a longer post.

Here’s a list of some website we may want to check if you’re interested on learning more:

If you have any questions, write a comment below and I’ll be happy to help you.

If you liked this post, remeber to share it and to subscribe to the site’s newsletter from the home page.

Anyway, the post is finished, have a good day and we will see next week!

From Zephyro it’s all, Bye!

Categories: Learn Java

Leave a Reply

%d bloggers like this: