Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  game freezes when dealing with selector  (Read 1073 times)
0 Members and 1 Guest are viewing this topic.
Offline Kova

Senior Member





« Posted 2006-05-19 17:21:07 »

Hello.
I don't know what to do anymore so I'm hoping this happened before and someone knows the solution.

I noticed that my game client freezes about every 10th time it connects to server. I'm calling connect method directly from main menu in main thread so when this occurs repaints don't work and minimize / restore gives emtpy gray screen. I must stop the jvm to shutdown the game. Soon I discovered on what line thing freezes... when connecting to server it uses SocketChannel.open(IP), sets blocking on channel to false, and finally registers for OP_READ. Game froze on:

1  
s_channel.register(selector, SelectionKey.OP_READ);


... I was thiniking maybe it's some synchronization problem since I have main menu thread, animation thread, server thread and packet sender thread. Allthough they didn't touch much, especially here, I deceided to synchronize it on selector:

1  
2  
3  
4  
5  
6  
7  
System.out.println("BBBBBBBBBBBBB");
    synchronized (selector) {
        System.out.println("CCCCCCCCCCCC");
   try {
            s_channel.register(selector, SelectionKey.OP_READ);
            System.out.println("DDDDDDDDDD");
    ...........................and so on............................


On my suprise the thing kept freezing as before, but the suprising part is that now it froze on
1  
synchronized (selector) {
line! Like wtf!? The B's are printed out, but the C's are not.... I'm feeling really dumb now, could this be something completely different? Why would it freeze on java keyword? Only thing in common in both freezes is selector reference.
Anyway I'm hoping I'm assuming it wrong and then it could be something else since I don't know how to fix this. Any similar experience? Any suggestions on ways how to debug this? Thank you.

Edit: forgot to explicitly mention, it does not throw an exception or native error, it just freezes in a way you can minimize / maximize it (gray screen) but all other things like clicks and others are gone as I can see.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2006-05-19 17:56:58 »

When you select() you synchronize on the object. Appearantly the register(..) method also synchronizes on the same lock, so you can only register (without waiting for a lock) when the lock is released by for example the select() method returned (processing I/O) or registering from inside the select-loop (which is basicly the same).

The fact that you see a 'freeze' on "synchronized( selector )" confirms this, and I kinda wonder why you are so stunned by that, as the behaviour of the synchronization keyword is the basics of multi-threaded synchonisation.

In short, you're trying to register(), which waits for a lock that is kept by another thread by blocking on select()

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #2 - Posted 2006-05-19 19:18:07 »

Do it without using threads.  Part of the point of NIO is asynchronous networking.  So you can run your network code in the main thread and it won't block.  This way you don't have to deal with errors that can be hard to find in threaded environments.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #3 - Posted 2006-05-20 12:02:55 »

Ah... that makes more sense... tnx Riven. I've read O'Reilly Java NIO 2 times (second time not so long ago while I was fixing this kind of bugs, this is my last one Smiley ), it dosen't have anything about select() locking selector or I missed / ignored it. Actually I'm not so suprised as it uses lock on selector now, but I was suprised it worked in 90% of time so I didn't bother to think of it that way. If it won't work at all (as expected with this) then I would find it.
So I guess solution would be to queue request for connect in same thread or do it before select() comes into action (or call wakeup() ... but that seems messy).

CaptainJester... it's kind of really hard and non OOP to do it all in one thread... have you tried it? Networking is obviusly extreamly slower then cpu so it could stall my app. frequently. Also it would be messy and hard to make since I would have to have game loop (animation), receiving data (selectNow() ), sending data every x ms and such in one thread. All that would have to go in game loop and that would be pretty confusing as I think of it. If someone has done it this way and it's good I would like to see it Smiley
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #4 - Posted 2006-05-20 19:37:21 »

Ah... that makes more sense... tnx Riven. I've read O'Reilly Java NIO 2 times (second time not so long ago while I was fixing this kind of bugs, this is my last one Smiley ), it dosen't have anything about select() locking selector

If that were true, I'd say you should bin the book, cos that's one of the most important things a book on NIO would have to talk about Sad. IIRC it's a generally OK book, so I'm *sure* it does in fact explain this at some point Cheesy

malloc will be first against the wall when the revolution comes...
Offline Kova

Senior Member





« Reply #5 - Posted 2006-05-21 17:16:40 »

I've checked to be sure, it does mention it but it's not within basics or using selectors, but in concurrency (kind of expected it in basics, so even If I read it I didn't payed so much attention to this I guess).
Anyway O'Reilly's Java NIO is a great book, I recommend it to anyone who wish to learn NIO.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

E.R. Fleming (17 views)
2014-07-29 03:07:13

E.R. Fleming (6 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!