THREAD-JAVA

 Write a program to illustrate creation of threads using runnable class. (start method start each of the newly created thread. Inside the run method there is sleep() for suspend the thread for 500 milliseconds).

Note:



PROGRAM:

Code 1: ThreadExample class

 

 package joda;

 class MyRunnable implements Runnable {

    @Override

    public void run() {

        try {

            System.out.println("Thread " + Thread.currentThread().getId() + " is running.");

            Thread.sleep(500);

            System.out.println("Thread " + Thread.currentThread().getId() + " is awake.");

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

    }

}

 ----------------------------------------------------------------

Code 2: ThreadExample class

 

 package joda;

 public class ThreadExample {

    public static void main(String[] args) {

        int numberOfThreads = 5;

         for (int i = 0; i < numberOfThreads; i++) {

            Thread thread = new Thread(new MyRunnable());

            thread.start();

        }

    }

}

 ----------------------------------------------------------------

 OUTPUT:

note:













No comments:

Post a Comment