• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i though it will always finish with a.i=0, but it's not, come somebody help me???
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maha, Ajith can you explain to me, I also stack, I think this question is similar to Valentine Mock Exam Q52.
Pls help us.
Thx.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright - this one is a bit tricky, but I believe I've found the answer. They key is to notice that each thread has it's own instance of an A object. Therefore, both threads could be executing a.aMethod() or a.bMethod() at the same time. Since i is a staic variable, both instances share that variable. Let's take this case:
0. A.i = 0
1. We create two threads, t1 and t2
2. We start both threads, t1 and t2
3. t1 invokes a.aMethod()
4. t1 increments i (i = 1)
5. t1 loses its timeslice before it can assign 1 to i (i it still 0)
6. t2 invokes a.aMethod()
7. t2 increments i (i = 1)
8. t2 assigns 1 to i
9. t2 loses its timeslice to t1
10. t1 assigns 1 to i
11. t1 invokes a.bMethod()
12. t1 decrements i (i = 0)
13. t1 assigns 0 to i
14. t1 loses its timeslice to t2
15. t2 invokes a.bMethod()
16. t2 decrements i (i = -1)
17. t2 assigns -1 to i
From this, you can see that, after one cycle of each thread invoking aMethod and bMethod, the value of i is no longer 0. One of the increments was lost due to the fact that the increment was performed but the thread was timesliced out prior to tha assignment being performed.
Similarly, you could propose a case in which a decrement would be lost, resulting in a positive value for i.
I doubt this conditions would occur very often. That's why the for loop is so large. You need to execute this code many times in order to get the timeslices to occur just right even a few times.
I hope that makes sense. :roll: If you have any more questions, please let me know.
Corey
[ June 04, 2002: Message edited by: Corey McGlone ]
 
Ependi Jusdi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it Corey, many thanks...
Regards,
au
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible that in the exam would appear a question like "Does the code below always print A.i=0?
[ June 04, 2002: Message edited by: Francisco A Guimaraes ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Francisco A Guimaraes:
Is it possible that in the exam would appear a question like "Does the code below always print A.i=0?
[ June 04, 2002: Message edited by: Francisco A Guimaraes ]


It is possible, but I have a feeling this question is a little more difficult than anything you'd see on the real exam.
Of course, on the exam, questions are multiple choice, so they'd probably give you a number of other options, such as "Compiler Error", etc.
Corey
 
There's a city wid manhunt for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic