Tuesday, 29 November 2016

Program to create two threads, one will print numbers in original order and other in reverse order from 1 to 50

To accomplish this task we have created two thread classes Original and Reverse which prints numbers from 1 to 50 in original and reverse order respectively.


PROGRAM
class Original extends Thread {
 
 public void run() {
  
  for(int i=1; i<=50; i++) {
   System.out.println("Original Order Thread : " + i);
  }
 }
}

class Reverse extends Thread {
 
 public void run() {
  
  for(int i=50; i>0; i--) {
   System.out.println("Reverse Order Thread : " + i);
  }
 }
}

public class OriginalReverseThread {

 public static void main(String[] args) {
  
  new Original().start();
  new Reverse().start();
 }
}
OUTPUT
C:\>javac OriginalReverseThread.java
C:\>java OriginalReverseThread
Original Order Thread : 1
Original Order Thread : 2
Original Order Thread : 3
Original Order Thread : 4
Original Order Thread : 5
Original Order Thread : 6
Original Order Thread : 7
Original Order Thread : 8
Original Order Thread : 9
Original Order Thread : 10
Original Order Thread : 11
Original Order Thread : 12
Original Order Thread : 13
Original Order Thread : 14
Original Order Thread : 15
Original Order Thread : 16
Original Order Thread : 17
Original Order Thread : 18
Original Order Thread : 19
Original Order Thread : 20
Original Order Thread : 21
Original Order Thread : 22
Original Order Thread : 23
Original Order Thread : 24
Original Order Thread : 25
Original Order Thread : 26
Original Order Thread : 27
Original Order Thread : 28
Original Order Thread : 29
Original Order Thread : 30
Original Order Thread : 31
Original Order Thread : 32
Original Order Thread : 33
Original Order Thread : 34
Original Order Thread : 35
Original Order Thread : 36
Original Order Thread : 37
Original Order Thread : 38
Original Order Thread : 39
Original Order Thread : 40
Original Order Thread : 41
Original Order Thread : 42
Original Order Thread : 43
Original Order Thread : 44
Original Order Thread : 45
Original Order Thread : 46
Original Order Thread : 47
Original Order Thread : 48
Original Order Thread : 49
Original Order Thread : 50
Reverse Order Thread : 50
Reverse Order Thread : 49
Reverse Order Thread : 48
Reverse Order Thread : 47
Reverse Order Thread : 46
Reverse Order Thread : 45
Reverse Order Thread : 44
Reverse Order Thread : 43
Reverse Order Thread : 42
Reverse Order Thread : 41
Reverse Order Thread : 40
Reverse Order Thread : 39
Reverse Order Thread : 38
Reverse Order Thread : 37
Reverse Order Thread : 36
Reverse Order Thread : 35
Reverse Order Thread : 34
Reverse Order Thread : 33
Reverse Order Thread : 32
Reverse Order Thread : 31
Reverse Order Thread : 30
Reverse Order Thread : 29
Reverse Order Thread : 28
Reverse Order Thread : 27
Reverse Order Thread : 26
Reverse Order Thread : 25
Reverse Order Thread : 24
Reverse Order Thread : 23
Reverse Order Thread : 22
Reverse Order Thread : 21
Reverse Order Thread : 20
Reverse Order Thread : 19
Reverse Order Thread : 18
Reverse Order Thread : 17
Reverse Order Thread : 16
Reverse Order Thread : 15
Reverse Order Thread : 14
Reverse Order Thread : 13
Reverse Order Thread : 12
Reverse Order Thread : 11
Reverse Order Thread : 10
Reverse Order Thread : 9
Reverse Order Thread : 8
Reverse Order Thread : 7
Reverse Order Thread : 6
Reverse Order Thread : 5
Reverse Order Thread : 4
Reverse Order Thread : 3
Reverse Order Thread : 2
Reverse Order Thread : 1

No comments:

Post a Comment