apl-mhd

Extend Thread Class

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