Write a program that executes two threads. One thread will print the even numbers and another thread will print odd numbers from 1 to 50.

 

AIM: Write a program that executes two threads. One thread will print the even numbers and another thread will print odd numbers from 1 to 50.

CODE:

//Write a program that executes two threads. One thread will print the even numbers and another thread will print odd numbers from 1 to 50.

class MyThread1 extends Thread{

            public void run(){

                        for(int i=1;i<=50;i++){

                                    if(i%2!=0){

                                                System.out.println("ODD: "+i);

                                    }

                        }

            }         

}

class MyThread2 extends Thread{

            public void run(){

                        for(int i=1;i<=50;i++){

                                    if(i%2==0){

                                                System.out.println("EVEN: "+i);

                                    }

                        }

            }         

}

class threadMain2{

            public static void main(String args[]){

                        MyThread1 t1 = new MyThread1();

                        t1.start();

                        try{

                        t1.join();}

                        catch(Exception ep){}

                        MyThread2 t2 = new MyThread2();

                        t2.start();

            }

}

OUTPUT:


Comments

Popular posts from this blog

Write an application that illustrates method overriding in the same package and different packages. Also demonstrate accessibility rules in inside and outside packages

Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, and Circle. Define one method area()in the abstract class and override this area() in these three subclasses to calculate for specific object.

Write a program in Java to perform I/O operations on a Text file.