jQuery in Action, 2nd edition*
The moose likes Threads and Synchronization and the fly likes Thread control using semaphores...well i think thats what i need Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread control using semaphores...well i think thats what i need" Watch "Thread control using semaphores...well i think thats what i need" New topic
Author

Thread control using semaphores...well i think thats what i need

Ben Corben
Greenhorn

Joined: Jul 20, 2010
Posts: 2
Ok, so im new here ^^ hello,

Ill start by explaining how my server works, each level creates the level below.

Mainthread

DatabaseGatewaythread

Client1thread Client2thread Client3thread Client4thread

DatabaseConn DatabaseConn DatabaseConn DatabaseConn


OK so now the problem i have is i want the client threads to share a semaphore so i can make sure only one Database connection
is present at one time. But the only way i can see of doing that is to have it in a parent thread which the child threads couldn't access, is
there anyway sibling threads could access the same semaphore?


if anyone understands this id appreciate any ideas, comments regarding my stupidity may also lead to results so are equally welcome.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18756
    
  40


A semaphore is just an object -- so it can be shared between threads, just like any other object.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

The easy way would be to centralize the place from where you get the connection (statically available class) and hence put your semaphore there.

Normally people use a datasource or a connection pool.

apigee, a better way to API!
Ben Corben
Greenhorn

Joined: Jul 20, 2010
Posts: 2
thanks guys, in the end i just declared the semaphore as public static and it worked. il look into "centralize the place from where you get the connection" as well.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread control using semaphores...well i think thats what i need