Program to create two threads, one thread will print numbers in ascending order where as second thread will print numbers in descending order between 1 to 15

In this program, we have created two threads, one thread will print numbers from 1 to 15 in ascending order and other thread will print numbers in descending order. The logic to do the same is shown in program. Note that the output of thread programs may vary.


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

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

public class AscendingDescendingThread {

 public static void main(String[] args) {
  
  new Ascending().start();
  new Descending().start();
 }
}
OUTPUT
C:\javac AscendingDescendingThread.java
C:\java AscendingDescendingThread
Ascending Thread : 1
Ascending Thread : 2
Ascending Thread : 3
Ascending Thread : 4
Ascending Thread : 5
Ascending Thread : 6
Ascending Thread : 7
Ascending Thread : 8
Ascending Thread : 9
Ascending Thread : 10
Ascending Thread : 11
Ascending Thread : 12
Ascending Thread : 13
Ascending Thread : 14
Ascending Thread : 15
Descending Thread : 15
Descending Thread : 14
Descending Thread : 13
Descending Thread : 12
Descending Thread : 11
Descending Thread : 10
Descending Thread : 9
Descending Thread : 8
Descending Thread : 7
Descending Thread : 6
Descending Thread : 5
Descending Thread : 4
Descending Thread : 3
Descending Thread : 2
Descending Thread : 1

Comments

Popular posts from this blog

Develop an Android application to display “Hello World!” on screen

Program to design an applet which draws a circle (having color BLUE) inside a triangle (having color YELLOW)

Build a Simple Android App to Display Student Details