Multi Thread
Multi Thread
package javaapplicationthread;
import java.io.*;
import java.util.*;
/**
*
* @author 40408104129
* @version 1.6
*/
/**
* thread constructor
* @param pr
* @param pw
*/
MyThread1(PipedReader pr, PipedWriter pw)
{
this.pr = pr;
this.pw = pw;
}
/**
* thread operation
*
*/
public void run()
{
try
{
int i;
for (i=2;i<65521;i++)
{
int j;
for(j=2; j<i; j++)
{
int n = i%j;
if (n==0)
{
break;
}
}
if(i == j)
{
pw.write(i);
}
}
pw.close();
}
catch (IOException e)
{
}
}
}
package javaapplicationthread;
import java.io.*;
/**
*
* @author 40408104129
* @version 1.6
*/
class MyThread2 extends Thread
{
private PipedReader pr;
private PipedWriter pw;
/**
* thread constructor
* @param pr
* @param pw
*/
MyThread2(PipedReader pr, PipedWriter pw)
{
this.pr = pr;
this.pw = pw;
}
/**
* thread operation
*
*/
public void run()
{
try
{
int f1, f2 = 1, f3 = 1;
while( f3<100000)
{
pw.write(f3);
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
pw.write(f3);
pw.close();
}
catch (IOException e)
{
}
}
}
package javaapplicationthread;
import java.io.*;
/**
*
* @author 40408104129
* @version 1.6
*/
public class MultithreadedProgram
{
/**
* main method
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
PipedWriter pw1 = new PipedWriter();
PipedReader pr1 = new PipedReader(pw1);
MyThread1 mt1 = new MyThread1(pr1, pw1);
System.out.println("The threads are generating Prime Numbers and Fibonacci Series
......Wait For some Time...... ");
mt1.start();
int m,n;
int k=0;
int[] item1=new int[100000];
while ((item1[k] = pr1.read()) != -1)
{
k++;
}
m=k;
pr1.close();
PipedWriter pw2 = new PipedWriter();
PipedReader pr2 = new PipedReader(pw2);
MyThread2 mt2 = new MyThread2(pr2, pw2);
mt2.start();
k=0;
int[] item2=new int[1000];
while ((item2[k] = pr2.read()) != -1)
{
k++;
}
n=k;
pr2.close();
int g,h,flag;
System.out.println("The Numbers that are both prime and Fibonacci");
for(g=0;g<n;g++)
{
flag=0;
for(h=0;h<m && flag==0;h++)
{
if(item2[g]==item1[h])
{
System.out.println(item2[g]);
flag=1;
}
}
}
}
}
javaapplicationthread
Class MultithreadedProgram
java.lang.Object
javaapplicationthread.MultithreadedProgram
Constructor Summary
MultithreadedProgram()
Method Summary
static void main(java.lang.String[] args)
main method
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString,
wait, wait, wait
Constructor Detail
MultithreadedProgram
public MultithreadedProgram()
Method Detail
main
public static void main(java.lang.String[] args)
throws java.lang.Exception
main method
Parameters:
args -
Throws:
java.lang.Exception
OUTPUT:
The threads are generating Prime Numbers and Fibonacci Series ......Wait For some Time......
The Numbers that are both prime and Fibonacci
2
3
5
13
89
233
1597
28657