• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Thread question

 
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, I was doing an mock from ExamLab and this question appeared:


It says that the answer is: 000000

Can anyone explain to me what happened?

Thanks
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matheus Souza wrote:It says that the answer is: 000000


That is correct.

Matheus Souza wrote:Can anyone explain to me what happened?


What exactly didn't you understand? Is it why 6 numbers are printed? Or is it why 0 is printed every time? Or both?

6 numbers were printed because, after execution of 1 thread (just one thread. not first or second - because we cannot guarantee which thread will execute first), length of s would be 1. After execution of 2 threads, length of s would be 2 and after execution of 3 threads length would be 3. Further, we are printing s for 3 times, hence, we get 1+2+3=6 numbers. This modification of s is thread safe because of synchronization(line 20)

Now, what happens to those numbers we passed during object creation (2,1 and 0)? Well, x is volatile. So, every time, a thread would refer copy from main memory (and not thread's local memory), so it will take 0 every time.

I hope this helps.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMO x variable is static so it's value the same for all objects. The last constructor set x to 0 and then started all threads, thats why result 000000.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rimantas grebliunas wrote:IMO x variable is static so it's value the same for all objects. The last constructor set x to 0 and then started all threads, thats why result 000000.


How did I miss this?

Thanks rimantas grebliunas for the correction.
 
Matheus Souza
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:

rimantas grebliunas wrote:IMO x variable is static so it's value the same for all objects. The last constructor set x to 0 and then started all threads, thats why result 000000.


How did I miss this?

Thanks rimantas grebliunas for the correction.



Thanks you guys, I got it. This is a very tricky question, insn't it? =]
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic