Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread and Pipes problem.

 
kunagu varun
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Hello ranchers please give an idea of how to implement above problem...
 
Campbell Ritchie
Sheriff
Pie
Posts: 48953
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a program which finds Fibonacci numbers and one to find prime numbers.

Really! You didn't think we would produce your code or do your homework, did you?

You will have to show us what you have got already, and we'll tell you what we think of it.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12124
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
start small. write a little code at a time. Every time I start a new program, I write exactly this much before I compile and test:


Once I'm sure that compiles and runs, I'll add a few more lines. Then I'd compile, test and debug those few lines.
 
kunagu varun
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel sorry for my irregular post ranchers...
I have gathered some information regarding pipedIO and written the following code for my problem stated in the previous post..

Please have a look at this code..


The following is the output which i am getting..
0
1
1
2
2
3
3
5
5
7
8
13
21
34
55
89
144
233
377
610
987
1597
0
1
2
3
1
2
3
5
8
13
Terminated
5
7
21
34
55
89
144
233
377
610
987
1597
0
1
1
2

Please solve this issue..
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me it looks like you are getting complicated when you don't need to. I thought this was an interesting problem, so I coded it earlier to see what I came up, after some research. I'd suggest making 3 classes: 1 that does the fibonacci numbers, one that figures the primes, and a third that will be used to compare the two. After you get those three classes functional, then I would look into integrating threads and pipes.

I suggest looking at this link, and the programs they designed.
 
kunagu varun
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link.. I will try as you suggested and post back here later..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic