0

I have this code that tick my JAVA game, but I need to create a new thread for each entity in the iterator.

public void tick(){
    player.tick();
    Iterator<Entity> it = entities.iterator();
    while (it.hasNext()) {
        Entity e = it.next();
        e.tick();
    }
}

I tried something like this, but it didn't do anything good at all.

    public void run(){
    entityThread = new Thread();
    entityThread.start();
    running = true;
    Iterator<Entity> it = entities.iterator();

    while(running) {
        player.tick();
        while (it.hasNext()) {
            Entity e = it.next();
            e.tick();

            try {
                entityThread.join();
                System.out.println("vyksta join");
                System.out.println(Thread.currentThread().getName());

            } catch (InterruptedException er) {
                er.printStackTrace();
            }
        }
    }
}

I thought about doing a threadpool, but there aren't many things in the iterator. 5 entities are there. How can I do this right?

zalias
  • 55
  • 7
  • 2
    What's your question? – user3707125 May 24 '20 at 18:25
  • @user3707125 sorry, the question is, how do I add multithreading to my program correctly? – zalias May 24 '20 at 18:27
  • 1
    You should take a look at how to create a thread, because you're not doing it correctly. Refer to something like [this](https://stackoverflow.com/questions/17758411/java-creating-a-new-thread) for more guidance. – SkryptX May 24 '20 at 18:29
  • 2
    What are you trying to achieve? If you have no clue about correct terminology or multithreading in Java, try at least your best explaining what you want. Not just asking *"how do I add multithreading to my program correctly?"*. – akuzminykh May 24 '20 at 18:32
  • @akuzminykh I need to create a new thread for each entity in the iterator and at the end of the program join all the threads. – zalias May 24 '20 at 18:38
  • @zalias And what is each thread supposed to do? Just exist? – akuzminykh May 24 '20 at 18:41
  • @akuzminykh each thread should have its own entity. Those entities interact with the player and world. Example: door entities waits for someone to open the door and etc – zalias May 24 '20 at 18:44
  • Does this answer your question? [wait until all threads finish their work in java](https://stackoverflow.com/questions/7939257/wait-until-all-threads-finish-their-work-in-java) – Abra May 24 '20 at 19:30
  • Entities aren't concurrent, actions are. It makes little sense to assign each entity it's own thread ;what about actions where two entities interact, like player and die? In what thread would the action run, player's or doo'rs? – daniu May 24 '20 at 20:12

2 Answers2

0

Here is the code for your example.

public void tick() {
  player.tick();
  Iterator<Entity> it = entities.iterator();
  while (it.hasNext()) {
    Entity e = it.next();
    Runnable task = () ->  e.tick();
    new Thread(task).start();
  }
}

In general you should avoid creating threads for each task. Think about using thread pool.

Pankaj
  • 2,220
  • 1
  • 19
  • 31
-1

Instead of iterating through the "it" Iterator to create a new Thread for each Entity, do this:

int n = Iterators.size(it);
Thread threads[] = new Thread[n];
for (int j = 0; j < n; j++) {
    threads[j] = new Thread("New Thread"){
    public void run(){
      //place what each thread should do in here
    }
    };
    threads[j].start();
}

//since you said at the end of program join all threads
for (int j = 0; j < n; j++) {
//place the following in a try catch
    threads[j].join();
}

Here is more information on how to create a Thread from the Thread class.

Mary X
  • 153
  • 2
  • 15