multi-threaded Java program

Ads

 
 

Share on Google+Share on Google+

Lokesh
multi-threaded Java program
1 Answer(s)      7 years ago
Posted in : Java Beginners
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.
Ads
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

Ads









Related Tutorials/Questions & Answers:
multi threaded program - Java Beginners
multi 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 Beginners
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
Advertisements
JDBC-ODBC Bridge multi-threaded.
JDBC-ODBC Bridge multi-threaded.  Is the JDBC-ODBC Bridge multi-threaded
Multi-threaded Programming
Multi-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 Beginners
java 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
i have problem in that program sir - JavaMail
i have problem in that program sir  1.Develop multi-threaded echo server and a corresponding GUI client in Java
Need to develop Java Program - Development process
Need to develop Java Program  Develop multi-threaded echo server and a corresponding GUI client in Java
Java JDBC
Java JDBC   Is the JDBC-ODBC Bridge multi-threaded
Need to develop Java Program - Development process
Need 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 programme - Java Beginners
Java programme  Develop a multi-threaded GUI application using java application
java server - Development process
java server  Develop multi-threaded echo server and a corresponding GUI client in java
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 - Development process
java  Develop multi-threaded echo server and a corresponding GUI client in Java
java program for
java program for   java program for printing documents,images and cards
java - Design concepts & design patterns
java   1.Develop multi-threaded echo server and a corresponding GUI client in java
Java Program
Java Program  A Java Program that print the data on the printer but buttons not to be printed
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 program
java program  write a program to print 1234 567 89 10
Java Program
Java Program  java program to insert row in excel sheet after identifying an object
java program
java program  java program to implement the reflection of a particular class details like constructor,methods and fields with its modifiers
java program
java program  Write a program to create an applet and display The message "welcome to java
java program
java program  how to write an addition program in java without using arithematic operator
java program
java program  write java program for constructor,overriding,overriding,exception handling
java program
java program  write a java program to display array list and calculate the average of given array
java program
java program  write a java program to display array list and calculate the average of given array
java program
java program  Write a java program to do matrix addition operation On two given matrices
java program
java program  Write a java program to find the number of Positive numbers in m* n matrix
java program
java program  hi friends how to make a java program for getting non prime odd numbers in a given series
java program
java 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 program
java 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

Ads

 
Advertisement null

Ads