• 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

Sharing variables among classes?(easy question)

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody,
i have a quite easy problem (i think) but i don�t know how to solve it!! (and it anoys me way too much!). I have some classes, which are able to create and destroy threads. The maximum number of threads should be 20, so these classes should share a variable holding the current number of threads, adding or substracting as they are created or destroyed, and in order to modify that value they should use a mutex.
And that�s my problem: how can i do so all the files, all the classes "see" the same value of the variable holding the number of threads, and use the same mutex in order to access to that value?
Any example would be of great help!
Thank you so much!!
 
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gus Spain
I apologize that I dont understand what you mean with "mutex"..
About your problem, I think you can use class variable (static variable)..

Correct me if I am wrong
thanks
daniel
 
Gus Spain
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, ��ll try to make myself clear:
"a.java" is a server that can create up to 20 threads. In order to not going over 20, there�s this variable "number_threads", so every time a thread is created, it goes up by one.
"b.java" is the code of the thread, and if there�s an exception, or if it ends smoothly, it has to take one from "number_threads".
"c.java" is another thread that cooperates with "b.java" with the same conditions for the thread to end.
So i need that all the three of them (a.java, b.java and c.java) access to number_thread to modify its value, using a mutex (MUtual-EXclusive code, like a semaphore). My question is, how can i do so the three files share the same variable, each of them seeing the new value very time is modified, and how can they share the semaphore (mutex)?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Threads and Synchronization forum...
[ October 25, 2004: Message edited by: Dirk Schreckmann ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic