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
Post a Comment