jQuery in Action, 3rd edition
The moose likes Sockets and Internet Protocols and the fly likes networking and threading! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "networking and threading!" Watch "networking and threading!" New topic

networking and threading!

kriti sharma
Ranch Hand

Joined: Feb 16, 2001
Posts: 160
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!
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

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.

[How To Ask Questions On JavaRanch]
kriti sharma
Ranch Hand

Joined: Feb 16, 2001
Posts: 160
you said exactly how he application is behaving. i will organise and post the code soon.
kriti sharma
Ranch Hand

Joined: Feb 16, 2001
Posts: 160
my problem appears to be solved. thanks.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Care to share the solution?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link: http://aspose.com/file-tools
subject: networking and threading!
It's not a secret anymore!