andari3107

multithreading

Feb 6th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. public class MultiThread {
  2.     public MultiThread() {
  3.     }
  4.  
  5.     public static void main(String[] args){
  6.         new NewThread();
  7.         try{
  8.             for (int i=5; i>0; i--){
  9.                 System.out.println("Main Thread : "+ i);
  10.                 Thread.sleep(100);
  11.             }
  12.         } catch (InterruptedException e){
  13.             System.out.println("Main thread interrupted.");
  14.         }
  15.         System.out.println("Main thread exiting.");
  16.     }
  17.  
  18.     static class NewThread extends Thread{
  19.         NewThread() {
  20.             super("Demo Thread");
  21.             System.out.println("Child Thread : " + this);
  22.             start();
  23.         }
  24.         public void run(){
  25.             try{
  26.                 for (int i=5; i>0; i--){
  27.                     System.out.println("Child Thread : "+ i);
  28.                     Thread.sleep(200);
  29.                 }
  30.             } catch (InterruptedException e){
  31.                 System.out.println("Child interrupted");
  32.             }
  33.             System.out.println("Exiting child thread.");
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment