Search This Blog

Sunday, 21 August 2022

B5. Java code for thread creation by implementing the Runnable Interface.

/* B5. 2019 syllabus Java code for thread creation by implementing the Runnable Interface */
import java.io.*;
import java.lang.*;
class Multi implements Runnable
{
    public void run()
    {
        try
        {
            // Display the running thread.
              System.out.println("Thread " + Thread.currentThread().getId()+ " is running");
        }
        catch (Exception e)
        {
            // Throw an exception
            System.out.println("Exception is caught");
        }
    }
}
// Main Class
class B5
{
    public static void main(String args[])
    {
        int n = 5; // Number of threads
        for (int i = 0; i < n; i++)
        {
            Thread obj = new Thread(new Multi());
            obj.start();
        }
    }
}

No comments :

Post a Comment

FREE Hit Counters