Multithreading example in Java using Runnable interface / Thread class

There are two ways to implement a thread in java:
– By implementing Runnable interface
– By extending Thread class
In Java only one class can be extended and hence using Runnable interface is suitable in most cases.

Multiple threads can be run at the same time to run tasks in parallel and save time.
Note: Be careful how many heavy threads you spawn though.

When threads are started a default priority of 5 (Thread.NORM_PRIORITY) is set automatically.
This can be modified by setting a value from 1 (Thread.MIN_PRIORITY) to 10 (Thread.MAX_PRIORITY).

Example 1: Runnable interface

class ClassOne implements Runnable
{  
    public void run()
    {
         System.out.printf( “Thread name: %s started %n”, Thread.currentThread().getName() );
         ThreadTest.sleep();
         System.out.printf( “Thread name: %s completed %n”, Thread.currentThread().getName() );
    }
|}

Example 2: Thread class

class ClassTwo extends Thread
{  
    public void run()
    {
        System.out.printf( “Thread name: %s started %n”, Thread.currentThread().getName() );
        ThreadTest.sleep();
        System.out.printf( “Thread name: %s completed %n”,    Thread.currentThread().getName() );|
    }
}

Example 3: Main method (in ThreadTest class) runs thread (which implements Runnable) in example 1

Thread name can also be passed in the Thread constructor. If thread name is not set, a default thread name is assigned.
thread = new Thread( classOne, “Thread Name” ); 

Thread priority can also be set like this in the below example before calling start().
thread.setPriority( Thread.MAX_PRIORITY );

public static void main ( String[] args )
{
    System.out.printf( “Thread name: %s started %n”, Thread.currentThread().getName() );          ClassOne classOne = new ClassOne();
    Thread thread;

    for ( int i = 1; i <= 5; i++ )
    {
        thread = new Thread( classOne ); 
        thread.start();
    }

    System.out.printf( “Thread name: %s completed %n”, Thread.currentThread().getName() );
}

Example 4: Main method (in ThreadTest class) runs thread (which extends Thread) in example 2

Thread priority can also be set like this in the below example before calling start().
classTwo.setPriority( 7 );

public static void main ( String[] args )
{
    System.out.printf( “Thread name: %s started %n”, Thread.currentThread().getName() );
    ClassTwo classTwo;

    for ( int i = 1; i <= 5; i++ )
    {
        classTwo = new ClassTwo();
        classTwo.start();
    }

    System.out.printf( “Thread name: %s completed %n”, Thread.currentThread().getName() );
}

Example 5: Sleep method of 1 second (in ThreadTest class) just to demonstrate this example

public static void sleep()
{
    try
    {
        Thread.sleep( 1000 );
    }
    catch( InterruptedException e )
    {
        e.printStackTrace();
    }
}

Output: Runnable interface

Thread name: main started 
Thread name: Thread-0 started 
Thread name: main completed 
Thread name: Thread-1 started 
Thread name: Thread-2 started 
Thread name: Thread-4 started 
Thread name: Thread-3 started 
Thread name: Thread-4 completed 
Thread name: Thread-1 completed 
Thread name: Thread-2 completed 
Thread name: Thread-0 completed 
Thread name: Thread-3 completed 

Output: Thread class

Thread name: main started 
Thread name: Thread-0 started 
Thread name: Thread-1 started 
Thread name: main completed 
Thread name: Thread-2 started 
Thread name: Thread-3 started 
Thread name: Thread-4 started 
Thread name: Thread-1 completed 
Thread name: Thread-0 completed 
Thread name: Thread-2 completed 
Thread name: Thread-4 completed 
Thread name: Thread-3 completed 

Leave a Comment

Your email address will not be published. Required fields are marked *