0% found this document useful (0 votes)
29 views

Multi Thread

This document discusses a Java program that uses multithreading to generate prime numbers and Fibonacci series concurrently. It defines two thread classes (MyThread1 and MyThread2) that write their output to piped streams. The main method starts both threads, reads their output into arrays, and prints the numbers that are in both arrays (i.e. are both prime and Fibonacci).
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Multi Thread

This document discusses a Java program that uses multithreading to generate prime numbers and Fibonacci series concurrently. It defines two thread classes (MyThread1 and MyThread2) that write their output to piped streams. The main method starts both threads, reads their output into arrays, and prints the numbers that are in both arrays (i.e. are both prime and Fibonacci).
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAM: [MULTI THREADING]

package javaapplicationthread;
import java.io.*;
import java.util.*;

/**
*
* @author 40408104129
* @version 1.6
*/

class MyThread1 extends Thread


{
private PipedReader pr;
private PipedWriter pw;

/**
* 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;
}
}
}

}
}

JAVA DOC COMMENTS:

Package   Class  Use  Tree  Deprecated  Index  Help 


 PREV CLASS   NEXT CLASS FRAMES    NO FRAMES     All Classes
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD

javaapplicationthread
Class MultithreadedProgram
java.lang.Object
javaapplicationthread.MultithreadedProgram

public class MultithreadedProgram


extends java.lang.Object
Version:
1.6
Author:
40408104129

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

Package   Class  Use  Tree  Deprecated  Index  Help 


 PREV CLASS   NEXT CLASS FRAMES    NO FRAMES     All Classes
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD

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

You might also like