• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Thread

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. class Widget extends Thingee{
2. static private int widgetCount = 0;
3. public String wName;
4. int wNumber;
5.
6. private static synchronized int
addWidget(){
7. return ++widgetCount;
8. }
9. public Widget(){
10. wNumber = addWidget();
11. }
12. }
this code will give unique value of wNumber for each Widget object created in multithreaded environment.
my question is if i remove static keyword from addWidget method will it give unique values for wNumber???
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeena, I tried and continued to get unique numbers as long as widgetCount itself remained static. I ran a single-threaded version; even so, I'm not sure what the functional significance is of having addWidget declared as static. It's a private function and the only call to it is via the constructor, so you can't call the method without having an instance of Widget. Can someone explain why this method needs to be declared static?
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe because in a multithread environment more than one object might invoke the method at the same time and that would result in some increments to widgetCount being lost. But if the method is static and synchronized only one object can have access to it at a given time. Am I right?

Originally posted by Scott Appleton:
Jeena, I tried and continued to get unique numbers as long as widgetCount itself remained static. I ran a single-threaded version; even so, I'm not sure what the functional significance is of having addWidget declared as static. It's a private function and the only call to it is via the constructor, so you can't call the method without having an instance of Widget. Can someone explain why this method needs to be declared static?


 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. Since the variable being changed is static all the Widget objects will be accessing the one variable. Since the method is static all the Widget objects share the same method which is synchronized. So no matter how many Wdiget objects exist at one time, they will each get a unique widgetCount.
 
bacon. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic