What are Threads?
A thread — sometimes known as an execution context or a lightweight process–is a single sequential flow of control within a process. As a sequential flow of control, a thread must carve out some of its own resources within a running program (it must have its own execution stack and program counter for example).
The code running within the thread only works within that context. Thus, other texts use execution context as a synonym for thread.
Let’s understand and introduction to Java threads. Below Java Code will create and start 3 independent threads.
CrunchifyThreads.java
package crunchify.java.tutorials; /** * @author Crunchify.com * Simple Java Thread Example: Creating and Starting Threads */ public class CrunchifyThreads { public static void main(String args[]) { new crunchifyThreadTest("Facebook").start(); new crunchifyThreadTest("Twitter").start(); new crunchifyThreadTest("Google").start(); } } class crunchifyThreadTest extends Thread { public crunchifyThreadTest(String crunchifyString) { // super: Allocates a new Thread object. // This constructor has the same effect as Thread (null, null, name). super(crunchifyString); } public void run() { for (int counter = 1; counter <= 5; counter++) { System.out.println("Loop " + counter + ": " + getName() + ", ID: " + getId() + ", State: " + getState()); try { // random(): returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. sleep((int) (Math.random() * 2000)); } catch (InterruptedException e) { // printStackTrace(): Prints this throwable and its backtrace to the standard error stream. // This method prints a stack trace for this Throwable object on the error output stream that is the value of the field System.err. e.printStackTrace(); } } System.out.println("\n========= Test Finished for: " + getName() + "\n"); } }
The run()
method is the heart of any Thread–it’s where the action of the Thread takes place.
The run()
method of the ThreadTest class contains a for
loop that iterates 5 times. In each iteration the method displays the iteration number and the name of the Thread than sleeps for a random interval between 0 and 2 seconds.
After the loop has finished, the run()
method prints “Test Finished For: ” along with the name of the thread.
Another must read: How to Start/Spawn New Thread in Background in Java
Console output:
Just run above program as a Java Application and you should see result similar to this one.
Loop 1: Twitter, ID: 15, State: RUNNABLE Loop 1: Google, ID: 16, State: RUNNABLE Loop 1: Facebook, ID: 14, State: RUNNABLE Loop 2: Facebook, ID: 14, State: RUNNABLE Loop 2: Google, ID: 16, State: RUNNABLE Loop 2: Twitter, ID: 15, State: RUNNABLE Loop 3: Twitter, ID: 15, State: RUNNABLE Loop 4: Twitter, ID: 15, State: RUNNABLE Loop 3: Facebook, ID: 14, State: RUNNABLE Loop 5: Twitter, ID: 15, State: RUNNABLE Loop 3: Google, ID: 16, State: RUNNABLE ========= Test Finished for: Twitter Loop 4: Facebook, ID: 14, State: RUNNABLE Loop 4: Google, ID: 16, State: RUNNABLE Loop 5: Google, ID: 16, State: RUNNABLE ========= Test Finished for: Google Loop 5: Facebook, ID: 14, State: RUNNABLE ========= Test Finished for: Facebook Process finished with exit code 0
Hope this helps you understand threading concept in Java.