The moose likes Sockets and Internet Protocols and the fly likes getOutputStream.write() > blocking!? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "getOutputStream.write() > blocking!?" Watch "getOutputStream.write() > blocking!?" New topic

getOutputStream.write() > blocking!?

Jean-Michel Vilain

Joined: Aug 06, 2010
Posts: 17

I'm working on a large multiplayer game, client-server, home made protocol, lots of Client threads inside the server. And 1 scheduler to avoid troubles.
But.. My scheduler often gets blocked. Thanks to jstack, I was able to discover where the block happens. Here's the stack:
"WorldScheduler" prio=10 tid=0x00007ff1f83b0800 nid=0x2eb4 runnable [0x00007ff1e2d91000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:141)
at faeria.world.Client.receiveMessage(Client.java:147)
at faeria.world.Account.receiveMessage(Account.java:92)
at faeria.world.Data.sendProperty(Data.java:51)
at faeria.world.DataRoom.enter(DataRoom.java:28)
at faeria.world.action.SignIn.enterSessionDataRooms(SignIn.java:110)
at faeria.world.action.SignIn.concreteRun(SignIn.java:86)
at faeria.world.action.WorldAction.run(WorldAction.java:16)
at faeria.world.action.WorldScheduler.run(WorldScheduler.java:26)
at java.lang.Thread.run(Thread.java:724)

Here's Client.receiveMessage, totally innocent except that it's called the scheduler thread... :

What the hell? How is a write supposed to block? Or at least lag for a very long time? And it happens so rarely, maybe 1 time over 100.000.
I missed something I guess. When it happens, I'm forced to kill and restart the server ASAP because there are tons of players blocked.
Anyone has an explanation on this? I'm adding a log to make sure the string I try to write isn't infinetly long...

Thanks a lot for your time guys.

EDIT: if write can be a blocking function, or can take long, then I should rewrite my Client class.
The scheduler thread would have to delegate the write to Client thread. But currently Client threads are used ONLY for reading, yes read is blocking which is natural.
Will I have to use an additional thread, thus 2 per Client, one for the blocking read, and one for the blocking write? Or is there anyway to code that with a single thread?

Designer and developer on Faƫria: Strategy Card Game (www.faeria.net). The server is 100% Java.
@Jiem_ on Twitter (http://twitter.com/Jiem_)
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

There is no reason to think that writing over a socket would not be blocking. Indeed your data shows that it does in fact block. The problem is that the OS will only be able to buffer so much TCP data. If you have a lot of traffic and writing takes time to send or confirm then yes, you will fill that buffer and should expect to block the thread trying to write the data.

Jean-Michel Vilain

Joined: Aug 06, 2010
Posts: 17

Thanks Steve.
Then I have no choice but to implement NIO.
I agree. Here's the link: http://aspose.com/file-tools
subject: getOutputStream.write() > blocking!?