Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two things "at the same time"

 
John McDave
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would threads not work?
 
John McDave
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic