Tuesday, 29 November 2016

Program to create two threads, one for Prime number and other for Non-prime number

This is a simple program which prints prime and non-prime numbers from 1 to 10. 


PROGRAM
class Prime extends Thread {
 
 public void run() {
  
  for(int i=1; i<=10; i++) {
   if(i==2 || i==3 || i==5 || i==7)
    System.out.println("Prime no. : " + i);
  }
 }
}

class NonPrime extends Thread {
 
 public void run() {
  
  for(int i=1; i<=10; i++) {
   if(i==4 || i==6 || i==8 || i==9 || i==10)
    System.out.println("Non-Prime no. : " + i);
  }
 }
}

public class PrimeNotPrimeThread {

 public static void main(String[] args) {
  
  new Prime().start();
  new NonPrime().start();
 }
}
OUTPUT
C:\>javac PrimeNotPrimeThread.java
C:\>java PrimeNotPrimeThread
Prime no. : 2
Prime no. : 3
Prime no. : 5
Prime no. : 7
Non-Prime no. : 4
Non-Prime no. : 6
Non-Prime no. : 8
Non-Prime no. : 9
Non-Prime no. : 10

No comments:

Post a Comment