Advertisement
apl-mhd

ImpleMentRunableClass

Jan 4th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. class NewThread implements Runnable{
  5.  
  6.     NewThread(){
  7.  
  8.         Thread t = new Thread(this,"Demo Thread");
  9.         System.out.println("child thread");
  10.  
  11.         t.start();
  12.     }
  13.  
  14.  
  15.     @Override
  16.     public void run() {
  17.  
  18.         for (int i=0; i<5; i++){
  19.  
  20.             try {
  21.                 System.out.println(Thread.currentThread().getName()+ i);
  22.                 Thread.sleep(1000);
  23.             } catch (InterruptedException e) {
  24.                 e.printStackTrace();
  25.             }
  26.  
  27.         }
  28.  
  29.         System.out.println("end Child");
  30.     }
  31. }
  32.  
  33. public class ThreadDemo {
  34.  
  35.     public static void main(String[] args) {
  36.  
  37.  
  38.         new NewThread();
  39.  
  40.  
  41.         try {
  42.             for(int i=0; i<5; i++) {
  43.                 System.out.println("Main " + i);
  44.                 Thread.sleep(500);
  45.             }
  46.         } catch (InterruptedException e) {
  47.             e.printStackTrace();
  48.         }
  49.  
  50.  
  51.         System.out.println("end main");
  52.     }
  53.  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement