[Java] Concurrent Programming

Minho Jang
Javarevisited
Published in
3 min readJul 11, 2021

--

Users take it granted that their systems can do more than one thing at a time. They assume that they can continue to work in a word processor, while other applications download files, manage the print queue, and stream audio. … Software that can do such things is known as concurrent software

The Java Tutorials

What is Concurrent Software?

I could not find a better definition in regard to concurrent programming than the one written in The Java Tutorials. It is referred to as ‘multiple sequences of operations run in overlapping periods of time’. Java supports concurrency with Thread class.

What is a Thread?

A thread is a thread of execution in a program. The JVM allows an application to have multiple threads of execution running concurrently. For a deep understanding of the multi-thread environment that Java provides, you need to know how threads interact with each other.

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon.

Daemon thread is a low-priority thread that runs in the background to perform tasks such as garbage collection. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread and is a daemon thread if and only if the creating thread is a daemon.

How Is a Thread Created?

An application that creates an instance of Thread must provide the code that will run in that thread. There are two ways to do this :

One way is to make a subclass that extends Thread , and to override its run method. Or, you can provide a Runnable object directly as a parameter. The Runnable the interface defines a single method, run , which contains the execution code.

The Runnable object is passed to the Thread constructor, as in the example. Which one looks better? Employing the Runnable object is more general, according to the ORACLE documentation, because you are able to extend a class other than Thread.

Operations of Thread

Pausing Execution w/ Sleep

Thread.sleep() causes the current thread to suspend execution for a given period. When the method that is running is asked to be terminated by interrupts, the Thread throws InterruptedException , which usually occurs when a thread interrupts another active thread.

Interrupting Execution w/ Interrupt

Thread.interrupt() , unlike Thread.sleep , causes the current thread to stop execution and do something else. A thread sends an interrupt by invoking an interrupt on the Thread object for the thread to be interrupted.

Waiting For Another Thread w/ Join

The join method allows one thread to wait for another to complete its operation. If newThread is the thread currently working, newThread.join causes the execution to join the main thread. Then, the main thread pauses its operation until the new thread terminates.

Result

The Thread the object was designed in Java to let developers handle as many operations as they need. So, it’s up to them to decide how many threads will be created, respond to an interrupt, and work interactively for a specified time.

However, as the number of threads increases, managing them correspondingly becomes tricky, which requires another object to integrate them all together. In the next article, we will be talking about the object.

Thanks for reading this post.

References

--

--

Minho Jang
Javarevisited

Backend Developer, Writer, and Lifelong Learner