• Post Reply Bookmark Topic Watch Topic
  • New Topic

Determine who is the winner?

 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am working on interview preparation and found following problem. I tried to do implementation for the same but having some doubt. Please
help me to clarify:

Problem: Design the below scenario in Java. In an Olympic event there is a running track and it is used for 400m race. You have 10 participants.
When the event start , capture the time taken by each participants. Determine who is the winner in each event.

My doubt: Whether all the threads started from the same time. I mean no thread should have head start so that exact difference can be calculated.






 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am thinking in the loop below threads are not starting together.

 
Henry Wong
author
Sheriff
Posts: 22528
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:
My doubt: Whether all the threads started from the same time. I mean no thread should have head start so that exact difference can be calculated.


Well, that depends on what you mean by "started [at] the same time". If you mean that if there was a mechanism that told you when something started, you would not be able to tell the difference, then yeah, they started at pretty much the same time. The processor, the OS scheduler, etc., are fast enough running the tasks, that you probably can't tell the difference.

Tushar Goel wrote:I am thinking in the loop below threads are not starting together.



On the other hand, if you mean the exact same time, down to the CPU cycle, then there are lots and lots of stuff that can be an issue. Are there enough processor cores to run the threads individually? Are the OS setup, and other applications setup done to not interfere, and allow the threads to run simultaneously? etc. etc. etc. And of course, what you pointed out.

Henry
 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry but i mean like in "Countdown latch" a gate is opened when an event is happened and all the threads which were waiting
able to enter into the gate. In this case no thread is having advantage of being started earlier. So like in this case as well is the same?
 
Henry Wong
author
Sheriff
Posts: 22528
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main use case for a countdown latch is when you have lots of threads that isn't allowed to start until all the threads are ready. Or perhaps, there are many phases to the processing, and threads aren't allowed to start a phase until all the threads are finished with the previous phase.

There is no black magic to the release of the threads -- a flag is set, and a notification is send. And these threads aren't spinning a core waiting for the flag; they have to be woken up, context switched in, etc. There is no guaranteed that they will be started at the *exact* same time; and even if they were (or attempted by spinning), there is no guarantee that the OS scheduler, processor, etc. can make that guarantee.

Henry
 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it.. Thanks Henry..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!