3

I want to make a system, so that there are certain tasks. For example, let's talk about a game. I want to make it so there are 100+ tasks doing different things, but when the player's magic level is 5, it will do the magic task, if the player's fighting skill is level 5, it will fight. I have that already, however here is the catch. I want to make it so once the task executes, it will have an 'ending'. So, it will do something before it finally gets killed.

My code so far:

for (GameTask a : s.gameTasks) {
    if (a != null) {
        if (a.validate()) {
            a.execute();
        }
    }
}

It will loop all the tasks and execute them, however how can I implement an 'ending' to it, so that it will get ready for the next task? I hope I have written it clearly as English is not my first language.

tl;dr, I want to add an 'end' to each task so that it can be killed and can be ready for next task.

user131733
  • 31
  • 1
  • 2
    Can you explain what you mean by an ending of a task? Usually a task has ended when its `execute` method returns. Or did you have something else in mind? – Bart van Ingen Schenau May 18 '14 at 17:57

2 Answers2

1

I'm not sure what you mean with ending a task and be ready for the next, but it looks to me that you just want a way of stopping each task so it can continue with the next one in the loop.

If that's the case then you probably wan't to make the GameTask class implement Runnable so you can execute them using the Executors class, something like this:

    ExecutorService executor = Executors.newFixedThreadPool(5);
    for (GameTask task : s.gameTasks) {
        executor.execute(task);
    }

Then, depending on what each task does, it might be more or less tricky to implement the end task method, but in principle the simplest way is that each task has a loop with a boolean that determines if it should continue being executed. You could then implement an "endTask" method in the GameTask that would set to false that variable.

You can see an example in this other question: https://stackoverflow.com/questions/94011/how-to-abort-a-thread-in-a-fast-and-clean-way-in-java

oli206
  • 111
  • 1
1

Is each GameTask less like a "task" and more like a "process"? Usually I think of a task as doing one thing and then stopping. It's just one step in a larger process. If you want to add more than one step to your GameTasks, then they become like little processes.

There are lots of different ways to address this, but one idea is to make each GameTask into a finite state machine -- that is, the "task" is in some state, and takes one step each round, and (maybe) transitions to a new state. When it finally gets to the terminal state, it can just fall out of the list.

(That's a really common way to handle independently running objects in game programming, but there are other ways to do it -- like when the task executes, it pushes itself or a new task onto the list for the next round.)

sea-rob
  • 6,841
  • 1
  • 24
  • 47