Showing posts with label JavaPgms. Show all posts
Showing posts with label JavaPgms. Show all posts
Java - Multithread - divisible by 5 and sum and average
/*
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>
*/
Java - Multithread - one to many Thread
/*
Write a java program that implements a
multi-threaded application that has three threads. First thread generates a
random integer every 1 second and if the value is even, second thread computes
the square of the number and prints. If the value is odd, the third thread will
print the value of cube of the number
*/
// Multithreaded application
// One thread to multiple threads
import java.util.*;
import java.io.*;
class Thd1 extends Thread
{
private
PipedWriter out1;
private
PipedWriter out2;
Thd1(ThdEven
teven, ThdOdd todd)
{
try
{
out1
= new PipedWriter(teven.getPipedReader());
out2
= new PipedWriter(todd.getPipedReader());
}
catch(Exception
e)
{
System.out.println("err"+e);
}
}
public
void run()
{
Thread
t = Thread.currentThread();
t.setName("Thd1");
System.out.println(t.getName()
+ " thread started");
int
i=1;
int
RN;
while(true)
{
try
{
if(i>5)
{
out1.close();
out2.close();
break;
}
RN
= (int)(Math.random()*10) + 1;
out1.write(RN);
out2.write(RN);
t.sleep(100);
}
catch(Exception e)
{
System.out.println("Thd1:"+e);
}
i++;
}
}
}
class ThdEven extends Thread
{
PipedReader
r1 = new PipedReader();
public
PipedReader getPipedReader()
{
return
r1;
}
public
void run()
{
Thread
t = Thread.currentThread();
t.setName("TE");
System.out.println(t.getName()
+ " thread started");
try
{
while(true)
{
int
rn = r1.read();
if(rn==-1)
break;
if(rn%2==0)
{
System.out.println(t.getName()+"
- even number "+rn+" - square = "+(rn*rn));
}
t.sleep(500);
}
}
catch(Exception
e)
{
System.out.println("ThdEven:"+e);
}
System.out.println(t.getName() + " thread
exiting");
}
}
class ThdOdd extends Thread
{
PipedReader
r2 = new PipedReader();
public
PipedReader getPipedReader()
{
return
r2;
}
public
void run()
{
Thread
t = Thread.currentThread();
t.setName("TO");
System.out.println(t.getName()
+ " thread started");
try
{
while(true)
{
int
rn=r2.read();
if(rn==-1)
break;
if(rn%2==1)
{
System.out.println(t.getName()+"
- odd number "+rn+" - cube = "+(rn*rn*rn));
}
t.sleep(500);
}
}
catch(Exception
e)
{
System.out.println("ThdOdd:"+e);
}
System.out.println(t.getName() + " thread
exiting");
}
}
public class Thread4
{
public
static void main(String[] args) throws Exception
{
Thread t=Thread.currentThread();
t.setName("Main");
System.out.println(t.getName() + " thread
Started...");
ThdEven
TE = new ThdEven();
ThdOdd
TO = new ThdOdd();
Thd1
t1 = new Thd1(TE, TO);
TE.start();
TO.start();
t1.start();
try
{
t.sleep(5000);
}
catch(Exception
e)
{}
System.out.println(t.getName() + " thread
exiting");
}
}
/*
Sample output
D:\oopjava>javac Thread4.java
D:\oopjava>java Thread4
Main thread Started...
TE thread started
Thd1 thread started
TO thread started
TO - odd number 7 - cube = 343
TE - even number 4 - square = 16
TO - odd number 9 - cube = 729
TO - odd number 5 - cube = 125
TE - even number 2 - square = 4
TO thread exiting
TE thread exiting
Main thread exiting
D:\oopjava>
Subscribe to:
Posts (Atom)