• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Two things "at the same time"

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would threads not work?
 
John McDave
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic