AlexRamallo
AlexRamallo

Reputation: 597

Pointer of an object in Java with multiple threads

I have two threads. One thread has an instance of myObjectManager. myObjectManager has a list of objects, and a method for retrieving an object( public myObjectClass getObjectById(int ID) )

I need the first thread to render an object in myObjectManagers list of objects, and the second thread to perform game logic and move it around etc.

This is what I tried

//thread 1:
m = myObjectManager.getObjectById(100);
m.render();

//thread 2:
m = myObjectManager.getObjectById(100);
m.rotate( m.getRotation() + 5 ); //increment rotation value

However, it seems that thread 1 has an instance of the object without the updated rotation. When I run it, the rendered object doesn't rotate, but when I make the second thread print out the rotation value it is rotated.

In C++ I would just make the function getObjectById() return a pointer to an instance of myObjectClass, but I'm not sure what exactly java does when I say "return myInstance;" How would I do something similar in java?

Sorry, I'm new to this language!

Upvotes: 3

Views: 1794

Answers (6)

Martin v. Löwis
Martin v. Löwis

Reputation: 127467

In Java, all Object variables are "pointers" (or "references", as people typically say). The problem must be elsewhere. My guess is that thread 1 has already rendered the object before thread 2 has even modified it.

Edit: Another theory: subsequent render() operations don't actually change the screen display. The rotation value is updated just fine - but it doesn't reflect to the display.

Upvotes: 4

Robin
Robin

Reputation: 24262

You have 2 potential problems, both of which have been stated here in different answers.

  1. You give no indication as to any control of ordering of your thread operations. Therefore the render may be occurring before the rotation update. This assumes that the the classes involved are in fact threadsafe and will behave as expected.
  2. If the classes are not threadsafe (i.e. synchronized in some way), then the updates to the rotation thread may never be seen in the rendering thread.

To know for sure we would have to see the source for the m class. Also, you may have issues with the getObjectById() as well if it is not threadsafe either.

Upvotes: 2

adamax
adamax

Reputation: 3865

Try marking your rotation variable in the object as volatile

Upvotes: 1

Axel Fontaine
Axel Fontaine

Reputation: 35169

All object references in Java, like your m variable, are in fact pointers.

So in your example, both m variables point to the same object.

Upvotes: 0

SLaks
SLaks

Reputation: 887459

All objects in Java are passed by reference.
It is impossible to write code that does what you're trying not to do.

Your first thread is probably running before the second thread.

Upvotes: 0

biziclop
biziclop

Reputation: 49744

The references (pointers) are alright but in Java each thread is allowed to make local copies of objects (think of it like a cache) they're working with and unless they are synchronized in some way, changes made by one thread may not be visible to the other.

This tutorial will hopefully help.

Upvotes: 3

Related Questions