Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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  
  MediaTracker question.  (Read 989 times)
0 Members and 1 Guest are viewing this topic.
Offline gbeebe
« Posted 2011-11-16 03:21:56 »

What method do you use for coming up with a numbering system for images when adding them to a tracker?  Right now I'm trying this hybrid Dewey Decimal system, which ends up having me looking up and down my code to make sure an ID isn't taken yet.  Then it occurred to me that if I had a pointer, I could use that as the ID, but couldn't find a method that gave me that.  Any ideas?
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #1 - Posted 2011-11-16 04:24:13 »

Increment a long  Smiley

Offline gbeebe
« Reply #2 - Posted 2011-11-16 04:44:41 »

Eclipse says it needs to be an int instead, but gave me a great idea of writing a small counting procedure:
1  
2  
3  
4  
public int nextID() {
      nextInt ++;
      return nextInt;
   }


Why I didn't think of this before... I don't know.
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 2011-11-16 05:25:02 »

If you ever plan on even reading that counter from multiple threads, you'd best make that an AtomicLong.
Offline gbeebe
« Reply #4 - Posted 2011-11-16 05:55:25 »

Are you saying that if one of my threads needs to use the MediaTracker, this method could pose a problem?  Even if I'm 100% sure that all other treads are done using this method?
Offline sproingie

JGO Kernel


Medals: 202



« Reply #5 - Posted 2011-11-17 00:14:21 »

Just making nextID() synchronized is also fine if nextInt is private.  If it's readable, it also should be volatile or you have no guarantees about not reading a stale id if it's updated by another thread.  You cannot be sure what's "done" if you haven't synchronized, because of reordering.  

The Atomic* classes are convenient for ensuring everyone gets a consistent view, and they do it a bit more efficiently than using synchronized blocks.  If you have a counter and you have multiple threads using it, make it atomic, full stop.

Just to scare you a little more: if you don't synchronize access to a long or double, it's possible to read a number where only half the bits are new and the other half are old, making your counter wildly off.  It's called "word tearing", and the JVM allows it to happen for those two datatypes.
Offline gbeebe
« Reply #6 - Posted 2011-11-17 03:08:31 »

So an atomic variable basically has a built in Mutex?
Offline sproingie

JGO Kernel


Medals: 202



« Reply #7 - Posted 2011-11-17 03:40:54 »

You can treat it like it has a mutex.  What it actually has is a volatile value internally that it updates with intrinsic compareAndSwap operations in sun.misc.unsafe.  If the CPU doesn't have CAS operations, it'll fall back to a mutex.
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #8 - Posted 2011-11-17 04:33:41 »

I'm not sure this will be an actual problem for you.  I'd be very surprised if you had multiple Threads trying to get at a single MediaTracker, seeing as they're just a means to an end to get Images.  Are you multi-threading your image loading?
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #9 - Posted 2011-11-17 04:38:28 »

EDIT: Nevermind.  Cool

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.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (35 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
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

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