Showing posts with label Threads. Show all posts
Showing posts with label Threads. Show all posts
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>
Java Threads - Producer consumer problem
// Thread Synchronization
// Producer Consumer Problem
class Q
{
int
n;
boolean
Flag = false;
synchronized
int get()
{
if(Flag
== false)
try
{
wait();
}
catch(InterruptedException
e)
{
System.out.println("InterruptedException
caught");
}
System.out.println("Received
: "+n);
Flag
= false;
notify();
return
n;
}
synchronized
void put(int n)
{
if(Flag
== true)
try
{
wait();
}
catch(InterruptedException
e)
{
System.out.println("InterruptedException
caught");
}
this.n
= n;
Flag
= true;
System.out.println("Sent
: "+n);
notify();
}
}
class Producer implements Runnable
{
Q
q;
Thread
t;
Producer(String
name, Q q)
{
this.q
= q;
t
= new Thread(this, name);
t.start();
}
public
void run()
{
int
i=1;
while(true)
q.put(i++);
}
}
class Consumer extends Thread
{
Q
q;
Consumer(String
name, Q q)
{
super(name);
this.start();
this.q
= q;
}
public
void run()
{
while(true)
q.get();
}
}
class PC
{
public
static void main(String as[])
{
Q
q1 = new Q();
Producer
p1 = new Producer("P", q1);
Consumer
c1 = new Consumer("C", q1);
}
}
Labels:
consumer,
CS8383,
Java,
JavaPgms,
producer,
Producer consumer,
Thread Synchronization,
Threads
Subscribe to:
Posts (Atom)