• Post Reply Bookmark Topic Watch Topic
  • New Topic

Add all even numbers, odd numbers in different threads  RSS feed

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Last week, an interviewer asked me to write a programme which add all even numbers from a list in one thread, odd numbers from same list
in different threads and largest of two in different thread. I was unable to make this but tried today. I run it 10 times it is giving correct
result, what do you say?



 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Odd numbers and even numbers? Do you mean values at odd‑numbered indices and values at even‑numbered indices?

Why do you want different threads?That code will work nicely with an array list and really badly with a linked list.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the requirements you have stated I think you have hugely overcomplicated it. Why not pass the same number list to two threads, and have each thread look for just the numbers they need to add up from that list? I don't even think you would need any explicit synchronization since the lists are not being modified.

If your implementation does actually meet the requirements then the requirements are strange. What you have basically implemented is an emulation of a single threaded iteration over a list using two threads. It is going to be less efficient than the single threaded equivalent.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Campbell mentioned, the definition of odd and even numbers are weird. Based on what the thread is doing, the odd numbers are 1, 2, and 11. And the even numbers are 5 and 10.

As for what the threads are doing. The odd thread is simply waiting. It isn't doing anything else ever.

The even thread is doing both the work for "even" and "odd" numbers. And when it is done with each iteration, it wakes up the odd thread, which promptly goes back to waiting.

Additionally, the main thread doesn't actually wait for the calculation to complete. It just waits for 1/10th of a second, and prints the result, regardless if it is finished or not.

Henry
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it, Henry? I didn't actually bother to read the whole of the original code.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hadn't read the code in depth either, but after reading it Henry is right. One thread gets stuck in an infinite while loop, does no work and ultimately waits forever.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, i mean values at even and odd index..
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did the requirements say one thread had to wait for the other thread to look at index i before it could look at index i+1?

I doubt it because that would just be sequential access emulating a single thread. As above, I think you just need to have each thread independently loop over the same list and select only the indexes it needs.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell and @Mike you are right. I made it complicated but interviewer asked me to use 3 thread instead of 1.

@Henry, you are right. But when i run this programme, it will not goes to waiting. Result is completed. Though i tried to print some debugging
notes and it is not printing in odd thread.




Can you please tell me how to correct this?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait for the other thread to look at index i before it could look at index i+1?

No, there is no such requirement. He just told me to use different threads.

I think you just need to have each thread independently loop over the same list and select only the indexes it needs.

Lemme try this.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I believe this task can be done safely without any explicit synchronization at all, even if you need to pass back the result of the sum to the main thread as long as it is after the two worker threads have terminated.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike i have followed your idea.. Is it correct?

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably yes. Might be better to have an enum OddEven, however.

Have you tried your code?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i did. Odd thread never runs. Even thread does all working but still not figured out how to correct it.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The standard way to wait for a thread to finish is simply to call its join() method. The way you've used is not standard at all. So I suggest you switch to using the standard way before trying to figure out what's wrong.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, i have updated my earlier code and add few more thing.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!