This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Game Development and the fly likes Two things Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Two things "at the same time"" Watch "Two things "at the same time"" New topic
Author

Two things "at the same time"

John McDave
Greenhorn

Joined: Nov 14, 2013
Posts: 6
Hello, this is my first post on the forums.

For practice and for fun I am trying to create a small game.

Currently, I loop through an array of objects and call a method to make each one of them do what they should do. For this question, say there are two objects: Object A and Object B. Object A moves to the right, Object B looks above itself to see if Object A is there, and if not it will delete itself. Object A is in position 0 of the array, object B in position 1.

The game starts
Object A moves right
Object B checks above it to see if Obj A is above it. It isn't, since it moved to the right, so Obj B removes itself.

The way it currently works, nothing can happen "at the same time." Even though Object A was there, since it moves just 1 turn before Object B, Object B doesn't notice it. This might be a stupid question, but is it possible to make two things do something at the same time? I've had a few ideas, but most of them would only work for specific cases and wouldn't be very convenient anyways. I know there are threads, which are kind of similar to what I'm asking for, but I'm pretty sure that wouldn't work for this.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 888
    
    9
Why would threads not work?
John McDave
Greenhorn

Joined: Nov 14, 2013
Posts: 6
E Armitage wrote:Why would threads not work?


If I'm correct, threads don't necessarily run at once, do they? For example, 2 threads running "at the same time" might do this:

method1 (run by thread 1)
do a
do b

method2 (run by thread 2)
do x

do a
do x
do b

Also, would it be practical to have a separate thread for each object if there were going to be a lot of objects?
John McDave
Greenhorn

Joined: Nov 14, 2013
Posts: 6
E Armitage wrote:Why would threads not work?


If I'm correct, threads don't necessarily run at once, do they? For example, 2 threads running "at the same time" might do this:

method1 (run by thread 1)
do a
do b

method2 (run by thread 2)
do x

do a
do x
do b

Also, would it be practical to have a separate thread for each object if there were going to be a lot of objects?
John McDave
Greenhorn

Joined: Nov 14, 2013
Posts: 6
E Armitage wrote:Why would threads not work?


If I'm correct, threads don't necessarily run at once, do they? For example, 2 threads running "at the same time" might do this:

method1 (run by thread 1)
do a
do b

method2 (run by thread 2)
do x

do a
do x
do b

Also, would it be practical to have a separate thread for each object if there were going to be a lot of objects?
John McDave
Greenhorn

Joined: Nov 14, 2013
Posts: 6
E Armitage wrote:Why would threads not work?


If I'm correct, threads don't necessarily run at once, do they? For example, 2 threads running "at the same time" might do this:

method1 (run by thread 1)
do a
do b

method2 (run by thread 2)
do x

do a
do x
do b

Also, would it be practical to have a separate thread for each object if there were going to be a lot of objects?

EDIT: Oops, sorry for replying multiple times, it told me that the servers were being worked on so I submitted it a few times thinking it hadn't worked before.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 888
    
    9
Threads are the closest you are going to get to concurrency in Java. Whether you need a thread for each object or not depends on your logic. It is possible to have only a few threads that are reused. See the concurrency api (http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html ) for how to do concurrency effectively.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

Threads may not run at the same time. However, they may eventually run.
So, for your case, it could be xab, axb or abx depends on thread scheduling.
You could create N separate threads for each object if you want N things eventually get executed.
But how they get executed may depends on number of processors the platform available.
Too many threads may eat up memory as well, so create threads only when needed.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

You may also want to consider having two sets of data - one that keeps track of where everything IS, and one that keeps track of where everything is GOING TO BE. So you iterate through the IS list. When A moves to the right, you update the GOING TO BE data. When you get to B doing its check, you look as where A IS, then update B's GOING TO BE.

Once you have gone through the entire list of objects, you then move stuff, and copy the GOING TO BE into the IS.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two things "at the same time"
 
Similar Threads
Hash tables and transposition tables
Sun Cirtification
Need help with Minimax Algorithm
Displaying a 'map'(grid) for a simple game I'm making -- most efficient way to use loops/Array.