Tech moves fast! Stay ahead of the curve with Techopedia!
Join nearly 200,000 subscribers who receive actionable tech insights from Techopedia.
A thread, in the context of Java, is the path followed when executing a program. It is a sequence of nested executed statements or method calls that allow multiple activities within a single process. All Java programs have at least one thread, known as the main thread, which is created by the Java Virtual Machine (JVM) at the program’s start, when the main method is invoked with the main thread.
Each thread comes with its own local variables, program counter, and stack, and is used to perform background processing that makes graphical user interface (GUI) application smoother. It does this by simplifying program logic, especially when multiple independent entities must run asynchronously.
In Java, creating a thread is accomplished by implementing an interface and extending a class. Every Java thread is created and controlled by the java.lang.thread class.
Like any sequential program, a single thread is constituted by a sequence and a single point of execution during its runtime. Unlike a program, however, it does not run on its own, but it’s run within the program.
Since they share both data and code (as they share the same address space), threads take advantage of the resources allocated for a program, and are therefore much more light-weighted than processes. In fact, threads are sometimes called “lightweight processes” since they are also sequential flows of control. Their cost of intercommunication is usually quite low as well, and switching between them is much easier and quicker.
In some texts, a thread is sometimes called execution context because it runs a code that works only within the context of the thread itself.
Whenever a thread is invoked, two paths will both execute it and follow its statement after the invocation.
Java is a multi-threaded application that allows multiple thread execution at any particular time. In a single-threaded application, only one thread is executed at a time because the application or program can handle only one task at a time. Each thread will have a separate stack and memory space.
For example, a single-threaded application may allow for the typing of words. However, this single thread requires an additional single thread allowing for the recording of keystrokes in order to type the words. Thus, a single-threaded application records the keystrokes, allowing the next single-threaded application (the typing of words) to follow.
However, a multi-threaded application allows for the handling of both tasks (recording and typing the keystrokes) within one application. Multiple threads can be invoked in a single program to perform different tasks at once, and they will all run at the same time. For example, in a HotJava Web browser, a file can be downloaded in the background while the user is interacting with the GUI in other ways, such as by playing a video on a media player or scrolling the page up and down.
When a thread is created, the Java program creates at least one main thread. Then, if more than one thread is created, every one of them is assigned a priority. The thread with higher priority is executed first, followed by lower-priority threads.
Since threads running concurrently might share the same data, two or more of them try to run an operation on the same variable at the same time. Since this occurrence will result in corrupt data, threads can be synchronized by allowing each one of them to lock that data until the operation is over. Once the first call returns, or a wait () is called within the method, the other threads that were denied access will resume working on that data.
The JVM stops executing threads under either of the following conditions:
The exit method has been invoked and authorized by the security manager
All of the daemon threads of the program have died
An Error or Exception has been thrown
A stop() method gets called by another thread
This definition was written in the context of Java