Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  Question about Multi-Threading and Working Together  (Read 3251 times)
0 Members and 1 Guest are viewing this topic.
Offline dr-snipe

Senior Newbie


Projects: 1



« Posted 2012-03-27 22:59:03 »

Hey guys, I'm pretty far into making a networked top-down shooter but I feel that my code has begun to suffer from crude code done by myself to keep the code working together on different threads. In simple terms, here's the main concern: I have a thread (clientListener) that receives messages from the server and that will thus have to interpret those messages. I have a main class called Main (clever eh?) that contains the main game loop and the menu system for connecting to the desired server. My problem is that I need to find out on the main thread if the client was accepted by the server which would be on the main thread to figure if it should then prepare the screen for running the game. What is a good way to do so?

Here's the current code running on the main thread
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
clientListener.setupForWait(); // Resets the waitingForResponse, acceptedByServer, etc...
statusLabel.setText("Contacting server....");
               
while(clientListener.waitingForResponse) { Thread.yield(); }
               
if(clientListener.acceptedByServer) {
   // Removes all of the connect components
   // Will load map instructed to by server and display that
   this.removeAll();
} else {
    statusLabel.setText("Rejected from Server: "+clientListener.reason);
}


It works, but it seems ugly. Any suggestions?

Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2012-03-27 23:22:44 »

1  
while(clientListener.waitingForResponse) { Thread.yield(); }


This is completely unnecessary in any properly designed threading app.  You should block on the response, which will pause the thread until the response is received.  The thing that most closely maps to this is a Condition, but those are quite low-level, and judging by the context of the rest of the code around it, is probably an inappropriate primitive to use.  You might be better served by using a BlockingQueue of Client objects that the listener enqueues.

Basically, you need to get familiar with the classes in java.util.concurrent and use only those for your concurrency needs.
Offline theagentd

« JGO Bitwise Duke »


Medals: 365
Projects: 2
Exp: 8 years



« Reply #2 - Posted 2012-03-27 23:44:24 »

And blocking infinitely might be slightly bad if you have a slow connection...

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

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-03-27 23:48:58 »

My initial reading of the code was that it was for a server accepting connections from clients, which line 2 alone should have told me was wrong... So yeah you probably don't need a queue, and frankly you don't need a worker thread at all if all you're doing anyway is spinning or blocking until connected. 

Otherwise, the advice to use java.util.concurrent still stands.

Offline dr-snipe

Senior Newbie


Projects: 1



« Reply #4 - Posted 2012-03-31 05:15:48 »

Hey guys thanks for the responses!

Yeah the Thread.yield() was just thrown in becasue I thought it would pause the thread and lower CPU usage but it sounds like that wouldn't be a good idea Smiley

Currently looking at concurrent threads. I'll update this and post back on how I restructure things.

Offline Roquen
« Reply #5 - Posted 2012-03-31 09:47:39 »

yield say: If there is another thread of equal or higher priority than me in my process wanting to run, then let them, otherwise I want to maintain control.  So it'll never lower CPU usage.  sleep(0) says stop me and put me back into the scheduler right now.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2012-04-02 09:18:30 »

Depending on the OS, yield==sleep(0)

IIRC this is the case for Windows - keep in mind that the behaviour is different since Windows Server 2003.

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

JGO Kernel


Medals: 202



« Reply #7 - Posted 2012-04-02 15:16:48 »

On Windows it's Sleep(0), on Linux it's sched_yield.  Not sure why they didn't use SwitchToThread on windows, but judging by the docs for SwitchToThread, it looks to have the same effect, namely that the thread loses the rest of its timeslice.  Regardless of the implementation, a loop that does nothing but yield will chew up far more CPU due to context switches than one that blocks on a condition instead.  You simply shouldn't see Thread.yield() in most code you run across.



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.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (21 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (49 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (105 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!