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

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative