Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getOutputStream.write() > blocking!?

 
Jean-Michel Vilain
Greenhorn
Posts: 27
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 27
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve.
Then I have no choice but to implement NIO.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic