Develop a program to create two threads ,one thread prints numbers from 1 to 10 & other thread prints numbers from 10-1 .first thread should transfer control to second thread often printing second number. - Helpwalaa - Free IT Updates & Opportunities

New Updates

Develop a program to create two threads ,one thread prints numbers from 1 to 10 & other thread prints numbers from 10-1 .first thread should transfer control to second thread often printing second number.


Program :- 

class th1 extends Thread
{
public void run()
{
for(int i=1;i<=10;i++)
{
while(ThMain.b1)
{}
System.out.println("Thread one: "+i);
ThMain.b1=true;
ThMain.b2=false;
}
}
}

class th2 extends Thread
{
public void run()
{
for(int i=10;i>=1;i--)
{
while(ThMain.b2)
{}
System.out.println("Thread two: "+i);
ThMain.b1=false;
ThMain.b2=true;
}
}
}

public class ThMain
{
public static boolean b1=false;
public static boolean b2=false;
public static void main(String a[])
{
th1 t1=new th1();
th2 t2=new th2();
t1.start();
t2.start();
}
}

______________________________________________________________________
Please Subscrbe channel on Youtube
link - youtube.com/CAPTURINGEYE

Most Popular