Amazon Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

sleep(t) make the current executing thread to sleep for 't' mill seconds. No matter what it will sleep for 't' milliseconds.

yield() make the current thread to temporarily pause and allows other thread to run. The thread which gets executed dependents on individual threads priority too. Current thread goes into runnable state and place itself in ready queue.

The difference here is that....
if when sleep is called current thread will go to sleep and other runnable thread will take over the processing if there are any. If there are no runnable thread then nothing happens current thread will continue to sleep for 't'.

But in case of yield() current thread give the control to other runnable threads, but if there are none, it takes the control immediately.

- master January 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

little correction to the above.... sleep(t) can be broken due to the interrupt.

- master January 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sleep Method puts thread to waiting state of the process life cycle. Once the thread has slept or waited in the waiting queue for specified amount of time it eneters the Runnable state to get executed.

Whereas by yielding a Thread goes into Runnable state leaving the running state to get executed.

- DJ May 31, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Also one more difference is , that if thread is running in the synchronized mode and then it goes to sleep mode it still holds the lock. However in the case of yield in the synchronized mode, the thread that is yielding has to release the lock. And therefore another thread can acquire the lock on that object

- jindal.bit June 12, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More