• 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

ch.9, problem 11 sierra/bates book - threads

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Below is the code for the problem from the book)

So I have 2 threads that are started, and both thread share the same run() function. It seems that, based on the answers, both threads share the instances of x and y declared in main. Why is that? Also, why declare x and y as final?







 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rachel, this is the property of inner classes - you cannot access non-final variable inside an inner class defined in a different method.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rachel Glenn wrote:It seems that, based on the answers, both threads share the instances of x and y declared in main. Why is that?


How many objects/instances of ThreadDemo were created in the main method? There were only two instances created so both the threads have to share them they don't have any other choice. If you were creating ThreadDemo objects in the main method then both threads would have got separate copies of the objects...
 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

Rachel Glenn wrote:It seems that, based on the answers, both threads share the instances of x and y declared in main. Why is that?


How many objects/instances of ThreadDemo were created in the main method? There were only two instances created so both the threads have to share them they don't have any other choice. If you were creating ThreadDemo objects in the main method then both threads would have got separate copies of the objects...



I am a little confused by your response. The main method created 2 Threads. The main method also created the x and y objects. Why do the threads share a copy of x and y?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic