Home Answers Viewqa Java-Beginners multi-threaded Java program

 
 


Lokesh
multi-threaded Java program
1 Answer(s)      2 years and 9 months 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.
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 Pages:
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
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
JDBC-ODBC Bridge multi-threaded.
JDBC-ODBC Bridge multi-threaded.  Is the JDBC-ODBC Bridge multi-threaded
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 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  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 JDBC
Java JDBC   Is the JDBC-ODBC Bridge multi-threaded
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 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 - Development process
java  Develop multi-threaded echo server and a corresponding GUI client in Java
java - Design concepts & design patterns
java   1.Develop multi-threaded echo server and a corresponding GUI client in java
java - Development process
java  Develop multi-threaded echo server and a corresponding GUI client in Java.and tell me the output.please reply sir
program
program  write a program different between to dates in terms of days in java
Apollo for Eclipse
for creating multi-threaded applications. Only the Professional Edition offers the breadth of advanced optimization, multi-threading, and processor support
C++ Compiler for Linux
for creating multi-threaded applications. Only the Professional Edition offers the breadth of advanced optimization, multi-threading, and processor support
program
program  WAP a java program to form 1/2+3/4+5/6+7/8 series
Java - Java Beginners
Java    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
program
program  explanation of program on extending thread class   Hi Friend, Please go through the following link: Java Threads Thanks
program
program  write a java program to input a no. and print wheather the no. is a special no or not. (special no. are those no whose factorial of the sum of digit of the no is same as the original
java - Java Beginners
java  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
i have problem in that program - JavaMail
i have problem in that program  1. Write a multi-threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number... paradigms of Java. Use JDBC to connect to a back-end database.  Hi Friend
java - Java Beginners
java  Design a thread-safe implementation of Queue class. Write a multi-threaded producer-consumer application that uses this Queue class
java - Java Beginners
java  Design a thread-safe implementation of Queue class. Write a multi-threaded producer-consumer application that uses this Queue class
program
is working in Java department and salary is 10000". Instantiate the Employee class
sirplz help me - JavaMail
sirplz help me  1.Develop multi-threaded echo server and a corresponding GUI client in Java
java multi thread - Development process
java multi thread  Develop multi-threaded echo server and a corresponding GUI client in Java.please immediately reply me sir.its very urgent sir
java - Java Beginners
java  Develop a multi-threaded GUI application of your choice.  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/applet/ClockApplet.shtml Hope that it will be helpful
java program for
java program for   java program for printing documents,images and cards
Thread in java
to as a single-threaded process, while a process with multiple threads is referred to as a multi-threaded process. For more details click on the following links...A thread is a lightweight process which exist within a program and executed
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  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  how to write an addition program in java without using arithematic operator
java program
java program  write a java program to display array list and calculate the average of given array
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 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
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
java program
java program   Write a program to find the difference between sum of the squares and the square of the sums of n numbers
java program
java program  write a program to create text area and display the various mouse handling events
java program
java 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 program
java 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 program
java 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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.