This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sharing variables among classes?(easy question)  RSS feed

 
Gus Spain
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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!!
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • 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
  • 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)?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Threads and Synchronization forum...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can share static data among many threads, but be sure to synchronize it:

Your clients would attempt to obtain permission and either succeed or fail. If they succeed, they'd have to be sure to release their claim by calling decrement later on. If they fail, they might retry. To make this more sophisticated you might make them wait until the number goes down. A good learning exercise fer sure.

A better approach would be to use a thread pool. You could create the 20 threads during initialization and use them all day long. You not only limite the number of threads running at once, but cut down the number of Thread creates and garbage collects throughout the day.

If you're in Java 5.0 look into the Executor and other classes in the new concurrency package. If you're pre-5.0 google for the Apache Commons Thread Pool or another well tested pooling library. The real strength of the thread pool approach is buy vs build - get some mature code in a hurry rather than spending a lot of time to write and debug new stuff.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, any object may be considered a mutex. You may use any of your objects as a mutex or create one just for synchronization... like so...


Now, you just have to make sure that every one of your threads, have the same instance of the mutex object.

Henry
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest that you not start down the road of using objects that have other purposes as mutexes. Just create a new object.

The 'mutex' activity is actually handled by the java language when you use the keyword 'synchronize.' I hear that java 1.5 actually has some mutex library though!?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!