/*
Write a java program for generating three
threads to perform the following operations
a. Getting
N numbers as input
b. Printing
the numbers divisible by five
c. Computing
the average.
*/
// Multithreaded application
// One thread to multiple threads
import java.util.*;
import java.io.*;
class Thd1 extends Thread
{
private
PipedWriter out1;
private
PipedWriter out2;
Scanner
s = new Scanner(System.in);
Thd1(ThdFive
t5, ThdSum ts)
{
try
{
out1
= new PipedWriter(t5.getPipedReader());
out2
= new PipedWriter(ts.getPipedReader());
}
catch(Exception
e)
{
System.out.println("err"+e);
}
}
public
void run()
{
Thread
t = Thread.currentThread();
t.setName("Thd1");
t.setPriority(4);
System.out.println(t.getName()
+ " thread started");
System.out.print("Enter
number of integers : ");
int
N = s.nextInt();
int
i=1;
int
RN;
while(true)
{
try
{
if(i>N)
{
out1.close();
out2.close();
break;
}
t.sleep(1000);
System.out.print("Enter
number : ");
RN
= s.nextInt();
out1.write(RN);
out2.write(RN);
}
catch(Exception e)
{
System.out.println("Thd1:"+e);
}
i++;
}
}
}
class ThdFive extends Thread
{
PipedReader
r1 = new PipedReader();
public
PipedReader getPipedReader()
{
return
r1;
}
public
void run()
{
Thread
t = Thread.currentThread();
t.setName("T5");
t.setPriority(6);
System.out.println(t.getName()
+ " thread started");
try
{
while(true)
{
int
rn = r1.read();
if(rn==-1)
break;
if(rn%5==0)
{
System.out.println(t.getName()+"
- Divisible by 5 - "+rn);
}
t.sleep(100);
}
}
catch(Exception
e)
{
System.out.println("ThdFive:"+e);
}
System.out.println(t.getName() + " thread
exiting");
}
}
class ThdSum extends Thread
{
PipedReader
r2 = new PipedReader();
public
PipedReader getPipedReader()
{
return
r2;
}
public
void run()
{
Thread
t = Thread.currentThread();
t.setName("TS");
System.out.println(t.getName()
+ " thread started");
int
counter = 0;
int
sum = 0;
try
{
while(true)
{
int
rn=r2.read();
if(rn==-1)
break;
sum
+= rn;
counter++;
t.sleep(100);
}
System.out.println("Number
of integers received = "+ counter);
System.out.println("Sum
= "+sum);
System.out.println("Average
= "+(double)(sum/counter));
}
catch(Exception
e)
{
System.out.println("ThdOdd:"+e);
}
System.out.println(t.getName() + " thread
exiting");
}
}
public class Multithreading
{
public
static void main(String[] args) throws Exception
{
Thread t=Thread.currentThread();
t.setName("Main");
System.out.println(t.getName() + " thread
Started...");
ThdFive
TF = new ThdFive();
ThdSum
TS = new ThdSum();
Thd1
t1 = new Thd1(TF, TS);
TF.start();
TS.start();
t1.start();
try
{
t.sleep(5000);
}
catch(Exception
e)
{}
}
}
/*
Sample output
D:\oopjava>javac Multithreading.java
D:\oopjava>java Multithreading
Main thread Started...
T5 thread started
Thd1 thread started
TS thread started
Enter number of integers : 3
Enter number : 55
T5 - Divisible by 5 - 55
Enter number : 32
Enter number : 35
T5 - Divisible by 5 - 35
Number of integers received = 3
T5 thread exiting
Sum = 122
Average = 40.0
TS thread exiting
D:\oopjava>
*/
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu