Write an application consisting of three threads including main thread in java. The other two thread name is Odd & Even respectively.The main thread reads & generates a set of rndom numbers & distinguish them as odd & even groups. The odd data & even data will be intermixing way by Odd & Even thread. Odd thread makes its work execute while Even thread goes on passing mode & vice-versa.(File must be save as: ExtendedThread.java)
import java.util.Random;
class Odd extends Thread
{
int k;
int d[]=new int[10];
String s;
Odd(int a[],int b,String st)
{
for(int i = 1; i<=b; i++)
d[i]=a[i];
k=b;
s=st;
start();
}
public void run()
{
try
{
for(int i = 1; i<=k; i++)
{
System.out.println(s + ": " +d[i]);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println( s+" thread interrupted" );
}
System.out.println("End of the seris of "+s);
}
}
class ExtendThread2
{
public static void main(String args[])
{
int c[]=new int[10];
int d[]=new int[10];
int a=0,k=0,m;
Random r=new Random();
for(int i = 1; i <=10; i++)
{
m=r.nextInt(10);
if(m%2==1)
{
a++;
c[a]=m;
}
else
{
k++;
d[k]=m;
}
}
//System.out.println("Even number: " +d[k]);
new Odd(c,a,"Odd number.");
new Odd(d,k,"Even number.");
}
}
////////////////////////////////////////////
Copy & paste this code in your Textpad & run, then you will get output.......
If you have any problem please comment below.........
Copy & paste this code in your Textpad & run, then you will get output.......
If you have any problem please comment below.........