multi-threaded Java program
Write a multi-threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number (some examples are 2, 3, 5, 13, etc.). Design a thread that generates prime numbers below 100,000 and writes them into a pipe. Design another thread that generates fibonacci numbers and writes them to another pipe. The main thread should read both the pipes to identify numbers common to both.
View Answers
August 20, 2010 at 1:45 PM
Hi Friend,
Try the following code:
import java.io.*;
import java.util.*;
class MyThread1 extends Thread {
private PipedReader pr;
private PipedWriter pw;
MyThread1(PipedReader pr, PipedWriter pw) {
this.pr = pr;
this.pw = pw;
}
public void run() {
try {
int i;
for (i=1;i<10;i++)
{
int j;
for (j=2; j<i; j++)
{
int n = i%j;
if (n==0)
{
break;
}
}
if(i == j)
{
pw.write(i+"\n");
}
}
pw.close();
} catch (IOException e) {
}
}
}
class MyThread2 extends Thread {
private PipedReader pr;
private PipedWriter pw;
MyThread2(PipedReader pr, PipedWriter pw) {
this.pr = pr;
this.pw = pw;
}
public void run() {
try {
int f1, f2 = 1, f3 = 1;
for (int i = 1; i <10; i++) {
pw.write(f3+"\n");
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
pw.close();
} catch (IOException e) {
}
}
}
class MultithreadedProgram {
public static void main(String[] args) throws Exception {
ArrayList list1=new ArrayList();
ArrayList list2=new ArrayList();
PipedWriter pw1 = new PipedWriter();
PipedReader pr1 = new PipedReader(pw1);
MyThread1 mt1 = new MyThread1(pr1, pw1);
System.out.println("Prime Numbers: ");
mt1.start();
int item1;
while ((item1 = pr1.read()) != -1){
char ch1=((char) item1);
System.out.print(Character.toString(ch1));
list1.add(Character.toString(ch1));
}
pr1.close();
PipedWriter pw2 = new PipedWriter();
PipedReader pr2 = new PipedReader(pw2);
MyThread2 mt2 = new MyThread2(pr2, pw2);
System.out.println("Fibonacci Numbers: ");
mt2.start();
int item2;
while ((item2 = pr2.read()) != -1){
char ch2=((char) item2);
System.out.print(Character.toString(ch2));
list2.add(Character.toString(ch2));
}
pr2.close();
System.out.println("Elements common to both lists are:");
list1.retainAll(list2);
for(int i=0;i<list1.size();i++){
System.out.print(list1.get(i));
}
}
}
Thanks
Related Tutorials/Questions & Answers:
multi threaded program - Java Beginnersmulti threaded program Hi i m developing a
multi thread
program to execute a real time algorith. I m using three threads. I want to share date between three threads .Is there a way to share data from one thread to another thread
multi-threaded Java program - Java Beginnersmulti-
threaded Java program Write a
multi-
threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number (some examples are 2, 3, 5, 13, etc.). Design a thread that generates prime numbers
Advertisements
Multi-threaded ProgrammingMulti-
threaded Programming Write a class resourceThread which has a method run which calls display method of resource class. Resource class has synchronized display method which prints the array string. Take a string
java program - Java Beginnersjava program 1.java
program to Develop a
multi-
threaded GUI application of your choice.
2.program to Develop a template for linked-list class along with its methods in
Java Java JDBC Java JDBC Is the JDBC-ODBC Bridge
multi-
threaded Need to develop Java Program - Development processNeed to develop
Java Program Write a
multi-
threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number (some examples are 2, 3, 5, 13, etc.). Design a thread that generates prime numbers
JAVA - Java Beginners methods in
Java.
2. Design a thread-safe implementation of Queue class. Write a
multi-
threaded
producer-consumer application that uses this Queue class.
3. Write a
multi-
threaded Java program to print all numbers below 100,000 that are both
JAVA - Java Beginners methods in
Java.
2. Design a thread-safe implementation of Queue class. Write a
multi-
threaded
producer-consumer application that uses this Queue class.
3. Write a
multi-
threaded Java program to print all numbers below 100,000 that are both
java program for java program for
java program for printing documents,images and cards
a Java program a
Java program
Write a
Java program to print even numbers from 2 to 1024?
Write a
Java program to print ? My Name is Mirza? 100 times?
Write a
Java program to print Fibonacci Series?
Write a
Java program to reverse a number
Java ProgramJava Program A
Java Program that print the data on the printer but buttons not to be printed
java programjava program write a
program to print
1234
567
89
10
java programjava program Write a
program to demonstrate the concept of various possible exceptions arising in a
Java Program and the ways to handle them.
... in
Java java programjava program how to write an addition
program in
java without using arithematic operator
java programjava program write a
java program to display array list and calculate the average of given array
java programjava program Write a
java program to do matrix addition operation
On two given matrices
java programjava program write
java program for constructor,overriding,overriding,exception handling
java programjava program write a
java program to display array list and calculate the average of given array
Java ProgramJava Program
java program to insert row in excel sheet after identifying an object
java programjava program
java program to implement the reflection of a particular class details like constructor,methods and fields with its modifiers
java programjava program Write a
java program to find the number of
Positive numbers in m* n matrix
java programjava program Write a
program to create an applet and display
The message "welcome to
java java programjava program hi friends how to make a
java program for getting non prime odd numbers in a given series
java programjava program Write a
program to find the difference between sum of the squares and the square of the sums of n numbers
java programjava program write a
program to create text area and display the various mouse handling events
java programjava program Develop the
program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The
program accpets three values: the pipes inner radius, its length, and the thickness of its wall
java programjava program . Develop the
program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The
program accpets three values: the pipes inner radius, its length, and the thickness of its wall
java programjava program . Write a
program which performs to raise a number to a power and returns the value. Provide a behavior to the
program so as to accept any type of numeric values and returns the results
Java programJava program Write a
program which performs to raise a number to a power and returns the value. Provide a behavior to the
program so as to accept any type of numeric values and returns the results
program in javaprogram in java write a reverse
program in
java using string buffer.the input and out put as follows.
input- hi good mornig
out put-ih doog ginrom
java programjava program write a
java program to compute area of a circle.square,rectangle.triangle,volume of a sphere ,cylinder and perimeter of cube using method over riding
java programjava program write a
java program to compute area of a circle.square,rectangle.triangle,volume of a sphere ,cylinder and perimeter of cube using method over riding
java program java program A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
java program to display above triangle
java programjava program write a
program to create server and client such that server receives data from client using BuuferedReader and sends reply to client using PrintStream
java programjava program write a
program to read 10 numbers from user and store it in a array. display the maximum n minimum number in the array
java programjava program write a
java program to create an array of size 10 by taking input from bufferreader and find out the average of array elements from that array
java programjava program i want a applet
program that accepts two input strings using tag and concatenate the strings and display it in status window.
please give mi he code for this in core
java java programjava program write a
java program to read a file which hold email address validate email address tohave formate @.* and replace all .com email address
java programjava program write a
java program to create an array of size 10 by taking input from bufferreader and find out the average of array elements from that array
java programjava program write a
java program to create an array of size 10 by taking input from bufferreader and find out the average of array elements from that array
Java ProgramJava Program I want to Write a
program in
JAVA to display to create a class called MATRIX using a two-dimensional array of integers. Perform the addition and subtraction of two matrices.
Help me
java programjava program i want a applet
program that accepts two input strings using tag and concatenate the strings and display it in status window.
please give mi he code for this in core
java