Amazon Interview Question for Software Engineer / Developers


Country: United States




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

It's not the case that every thread will have its own separate own copy. The variable is declared static, and so there is just one main copy of the variable, shared among all the instances. The issue here is that unless the variable is declared volatile, a thread may cache a local copy of the variable in a register and not synchronize that copy with the central copy in memory until the next time a synchronized statement occurs. It may also, however, not cache a local copy, or sync the local copy with the main copy any time before that, such as when the thread no longer needs to keep the variable in a register. So the thread will probably eventually see the new value of false, but unless you use a volatile variable or a synchronized block, it might not see it as soon as it's set, and there are no hard guarantees on when exactly it will see it.

- eugene.yarovoi March 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

isn't flagRun defined as class variable which means it is shared by all instances?

- Jay March 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Indeed... The code also seemed messed up....

- rayray81502 March 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Eugene is right!

docs.oracle.com/javase/7/docs/api/java/util/concurrent/package-summary.html#MemoryVisibility

- Pavan Dittakavi March 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The main problem is the thread is a deamon thread. if you call the main method, the main thread will exit quickly.

- Li Ping December 23, 2015 | 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