Runnable

Creating thread by implementing Runnable interface
1. Let your class implement “Runnable” interface.
2. Now override the “public void run()” method and write your logic there (This is the method which will be executed when this thread is started).
That’s it, now you can start this thread as given below
1. Create an object of the above class
2. Allocate a thread object for our thread
3. Call the method “start” on the allocated thread object.
Demo Code - Creating Thread by implementing “Runnable” interface
In this example, we will create two threads “FirstThread” and “SecondThread”. Both of these threads will display numbers 1, 2, 3…10 with a one second delay in displaying the next number. ThreadDemo class will be starting these threads “FirstThread” and “SecondThread”.
First Thread
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//This class is made as a thread by implementing "Runnable" interface.
 
public class FirstThread implements Runnable
{
 
  //This method will be executed when this thread is executed
  public void run()
  {
 
    //Looping from 1 to 10 to display numbers from 1 to 10
    for ( int i=1; i<=10; i++)
    {
        //Displaying the numbers from this thread
        System.out.println( "Messag from First Thread : " +i);
 
       /*taking a delay of one second before displaying next number
        *
        * "Thread.sleep(1000);" - when this statement is executed,
        * this thread will sleep for 1000 milliseconds (1 second)
        * before executing the next statement.
        *
        * Since we are making this thread to sleep for one second,
        * we need to handle "InterruptedException". Our thread
        * may throw this exception if it is interrupted while it
        * is sleeping.
        *
        */
        try
        {
           Thread.sleep (1000);
        }
        catch (InterruptedException interruptedException)
        {
           /*Interrupted exception will be thrown when a sleeping or waiting
            *thread is interrupted.
            */
            System.out.println( "First Thread is interrupted when it is sleeping" +interruptedException);
        }
    }
  }
}
Second Thread
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//This class is made as a thread by implementing "Runnable" interface.
public class SecondThread implements Runnable
{
 
   //This method will be executed when this thread is executed
   public void run()
   {
 
      //Looping from 1 to 10 to display numbers from 1 to 10
      for ( int i=1; i<=10; i++)
      {
         System.out.println( "Messag from Second Thread : " +i);
 
        /*taking a delay of one second before displaying next number
         *
         * "Thread.sleep(1000);" - when this statement is executed,
         * this thread will sleep for 1000 milliseconds (1 second)
         * before executing the next statement.
         *
         * Since we are making this thread to sleep for one second,
         * we need to handle "InterruptedException". Our thread
         * may throw this exception if it is interrupted while it
         * is sleeping.
         */
         try
         {
             Thread.sleep(1000);
         }
         catch (InterruptedException interruptedException)
         {
            /*Interrupted exception will be thrown when a sleeping or waiting
             * thread is interrupted.
             */
             System.out.println( "Second Thread is interrupted when it is sleeping" +interruptedException);
         }
      }
    }
}
Main Class which starts the “First Thread” and “Second Thread”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class ThreadDemo
{
     public static void main(String args[])
     {
        //Creating an object of the first thread
        FirstThread   firstThread = new FirstThread();
 
        //Creating an object of the Second thread
        SecondThread   secondThread = new SecondThread();
 
        //Starting the first thread
        Thread thread1 = new Thread(firstThread);
        thread1.start();
 
        //Starting the second thread
        Thread thread2 = new Thread(secondThread);
        thread2.start();
     }
}
Output of ThreadDemo
Output of ThreadDemo

 
 

No comments:

Post a Comment