Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  Weird Xp/Win7 bug  (Read 1034 times)
0 Members and 1 Guest are viewing this topic.
Offline ahottev

Senior Newbie





« Posted 2010-11-29 14:54:53 »


In my MainMenu class, i have this line, which waits for a player selection:

while(  (selectedWorld == -1)  &  (editSelectedWorld == -1)  ){}

In windows Xp, it works. Now, i've tried it on my laptop at home, using Win7, 64 bits, and there's no way it'll work. It simply doesn't seem to test either values.

But, if i add:

while(  (selectedWorld == -1)  &  (editSelectedWorld == -1)  ){

     System.out.println( selectedWorld + "," + editSelectedWorld );

}

It works.  Huh Huh
Is there any reason for that?
Offline cylab

JGO Ninja


Medals: 38



« Reply #1 - Posted 2010-11-29 15:34:47 »

Weird Xp/Win7 bug

It's not a bug, it's your own fault...

Is there any reason for that?

I guess this is a multithreaded application and the selectedWorld and editSelectedWorld variables are updated from another thread. Since you do not synchronize the access to the variables, you might just read out the values from the cpu-cache and not the actual ones (maybe because they are updated from the other cpu on your laptop - I bet you have a dual-core laptop...).

Anyway thread safe access to ints is afaik (at least theoretically) platform dependent and should be avoided. You could get away with declaring them volatile, but I wouldn't bet on it. Learn about thread safe programming using the synchronized keyword and you will be fine.

On a sidenote: the logical AND operator in java is &&. A single & is a binary AND, which might do something completely different than you expect...

Mathias - I Know What [you] Did Last Summer!
Offline ahottev

Senior Newbie





« Reply #2 - Posted 2010-11-29 16:40:19 »

It's not a bug, it's your own fault...

I guess this is a multithreaded application and the selectedWorld and editSelectedWorld variables are updated from another thread. Since you do not synchronize the access to the variables, you might just read out the values from the cpu-cache and not the actual ones (maybe because they are updated from the other cpu on your laptop - I bet you have a dual-core laptop...).

Anyway thread safe access to ints is afaik (at least theoretically) platform dependent and should be avoided. You could get away with declaring them volatile, but I wouldn't bet on it. Learn about thread safe programming using the synchronized keyword and you will be fine.

On a sidenote: the logical AND operator in java is &&. A single & is a binary AND, which might do something completely different than you expect...

Well, no, it's not a multithreaded application. My laptop is dual-core (win7) and my other pc is quad core (but it has winXP installed), but nothing is actually multithreaded in my application. Unless you mean windows decides to multi-thread things without me knowing it?

About the '&', the way i understood it, a binary AND would test both values before continuing where a logical AND &&  will only test the first one?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 38



« Reply #3 - Posted 2010-11-29 16:53:56 »

If it's not multithreaded, what is

1  
while(  (selectedWorld == -1)  &  (editSelectedWorld == -1)  ){}


supposed to do then?  Tongue

Quote
Unless you mean windows decides to multi-thread things without me knowing it?

Not Windows, but java if you use a Swing/AWT, because the event handling runs on it's own thread, the AWT Event Thread or EDT (Event Dispatcher Thread)

Quote
About the '&', the way i understood it, a binary AND would test both values before continuing where a logical AND &&  will only test the first one?

A binary AND combines bit-pattern, a logical AND combines boolean expressions. That are different things - don't use the one for the other. A logical AND will not test the second expression if the first one evaluates to false, simply because it doesn't have to - (false AND true) would be false in the end anyway. This is called shortcut evaluation and is a GoodThing (TM). It will however test the second expression, if there is a chance that the whole expression could evaluate to true.

Mathias - I Know What [you] Did Last Summer!
Offline ahottev

Senior Newbie





« Reply #4 - Posted 2010-11-29 17:20:42 »

If it's not multithreaded, what is

1  
while(  (selectedWorld == -1)  &  (editSelectedWorld == -1)  ){}


supposed to do then?  Tongue

Nothing Smiley  As you said, it's a Swing menu, so it's just waiting for the player to press either the world to play in, or select the editor to edit a a world. Once pressed, a JButton returns selectedWorld == 3 for example, if the user wishes to play in world 3.

Both values start a -1 since neither a world or an editor is selected.

Yeah, i know it's not the best approach, but at the time it seemed like a quick and working way of doing it...

Quote
Not Windows, but java if you use a Swing/AWT, because the event handling runs on it's own thread, the AWT Event Thread or EDT (Event Dispatcher Thread)

Hum... i'll have to check that now, thanks.
Offline cylab

JGO Ninja


Medals: 38



« Reply #5 - Posted 2010-11-29 18:13:15 »

Also doing this kind of loop drives your cpu-usage to 100% and makes your machine slow and slugish. You should use a Thread.sleep(100); or something inside the loop. Also make sure you are accessing the variables in a thread safe manner using synchronized(someFinalObject){yourCrossThreadVariableAccess} - but DON'T put the whole while-loop in a synchronized Wink.

Mathias - I Know What [you] Did Last Summer!
Online Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #6 - Posted 2010-11-29 18:53:03 »

In this instance I suspect an object lock (monitor) along with a wait/notify would be the correct solution.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline JL235

JGO Coder


Medals: 10



« Reply #7 - Posted 2010-11-30 11:26:34 »

As others have pointed out if your using Swing then your building a multi-threaded app. Typically you don't need to care (all your stuff is within action listeners called at the correct moment) but if your waiting on something then you do.

Someone suggested using Thread.sleep above, I second this.

Lots of naive programmers (including myself) also fall into the trap of using Thread.yield instead (thinking yielding to another thread will be better then sleeping); just as a word of advice make sure you don't do this either!. Yielding works fine on Vista and Win7 but on any other OS it's the same as spinning inside a while loop.

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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

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

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

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

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

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

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

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
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!