Tuesday, 29 November 2016

Write a program to create a thread to print all even nos. from 1 to 50 with delay to 100 after each number

This program will print all even numbers between 1 to 50 with 100ms delay after each number.


PROGRAM
class EvenNum1To50 extends Thread {
 
 public void run() {
  
  for(int i=1; i<=50; i++) {
   if(i%2 == 0)
    System.out.println("Even Thread: "+i);
   try {
    Thread.sleep(100);
   }
   catch (InterruptedException e) { }
  }
 }
}

public class EvenThreadWithDelay {

 public static void main(String[] args) {
 
  new EvenNum1To50().start();
 }
}
OUTPUT
C:\>javac EvenThreadWithDelay.java
C:\>java EvenThreadWithDelay
Even Thread: 2
Even Thread: 4
Even Thread: 6
Even Thread: 8
Even Thread: 10
Even Thread: 12
Even Thread: 14
Even Thread: 16
Even Thread: 18
Even Thread: 20
Even Thread: 22
Even Thread: 24
Even Thread: 26
Even Thread: 28
Even Thread: 30
Even Thread: 32
Even Thread: 34
Even Thread: 36
Even Thread: 38
Even Thread: 40
Even Thread: 42
Even Thread: 44
Even Thread: 46
Even Thread: 48
Even Thread: 50

No comments:

Post a Comment