Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

networking and threading!  RSS feed

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to design a mini database server. Machine that has the database will be called server, and the machine that sends request to it will be client.
When the server machine boots, a server socket is opened on the machine to listen to client requests for the database. The server.java class has the main method that opens the server socket. The main method then spawns threads for read and write operations. The database server application is so designed that when there is no or only read operation(s) going on on a file(flat file database), allow further read operations and if a write request is made this write thread must wait till all the read threads exit. Similarly if a write operation is going on, any other read or write request thread must wait.
A single client request may spawn multiple read, write threads on various files. Multiple clients may make simultaneous requests to the server. The problem is that, whenever a client has a blocked read or write thread, the next calling client is not granted a network connection to the database, i.e. the accept() method does not get called. This happens only because the control is not going back to the main thread. Whenever a thread has to wait, I make it sleep. This must automatically yield control to the main thread and let other clients connect. But it does not happen. In fact, if I have a client that has all read operations, and hence no thread is blocked, the next calling clients are accepted. But as soon as even a single thread of any one client has to wait, no next client is accepted. Although when the waiting thread gets the control in due time, the next client is connected.

I tried the same program for a test program where the main thread does not make network connections but runs a loop to print 50 numbers. In this case the main thread does resume control when a child thread gets blocked.

Can this issue be resolved? Please do help!
 
Bartender
Posts: 9548
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds to me like you are making the main thread wait rather than spawning off a write thread and making it wait. You will probably have to show us some code if you want more specific help.
 
kriti sharma
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you said exactly how he application is behaving. i will organise and post the code soon.
 
kriti sharma
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my problem appears to be solved. thanks.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Care to share the solution?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!