• 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

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Henry
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ben Corben
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic