Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (794)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
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 6034 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
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
} 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 »

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
« Reply #2 - Posted 2012-03-27 23:44:24 »

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

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

JGO Kernel

Medals: 518

« 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

« JGO Overlord »

Medals: 1364
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 rankings!
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  

hadezbladez (3180 views)
2018-11-16 13:46:03

hadezbladez (1149 views)
2018-11-16 13:41:33

hadezbladez (3150 views)
2018-11-16 13:35:35

hadezbladez (627 views)
2018-11-16 13:32:03

EgonOlsen (3916 views)
2018-06-10 19:43:48

EgonOlsen (4400 views)
2018-06-10 19:43:44

EgonOlsen (2620 views)
2018-06-10 19:43:20

DesertCoockie (3439 views)
2018-05-13 18:23:11

nelsongames (3570 views)
2018-04-24 18:15:36

nelsongames (4617 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45 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‑
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!