简体   繁体   中英

Schedule a task for events

I want to schedule a once time task, why and what is the case:

The Case I have an game with more than one event, for example a card game with 3 matches, each 'match' has the user to win 2 of the 3 games to win the event.

Match 1 best of 3 games Match 2 best of 3 games Match 3 best of 3 games

Now I would like to start each match every 3 minutes and each game will take 1 minute, for example

Hour - Game

  1. 00:00 Start Match 1 and start Game 1 of Match 1
  2. 00:01 End Game 1 of Match 1
  3. 00:01 Start Game 2 of Match 1
  4. 00:02 End Game 2 of Match 1
  5. 00:02 Start Game 3 of Match 1
  6. 00:03 End Game 3 of Match 1
  7. 00:03 End Match 1

Ofcourse every task has only to start once and will never ever happen again (it will happen again, but not with the same players/game ID/match ID)

So what I think I should do is to create a Match Taskwith a runable and that runable will create a Task for each game. After the game Task is finished I should shutdown that thread or is there something else how you can start something like the case above?

I had already read alot about the Timer en ScheduledExecutorService class, but which one of these is the best of is there another better example?

Thanks for your time!

edit:

Ofcourse always after you asked a question, you find some things. So I will post my class here, this is not the dynamic class, but a test class. The Runable runs now when I want it, the only question I have is, when the runable is executed and finished will he be removed from the thread. Or will the thread pull get full after 100.000.000 events of these?

 import java.util.Timer;
 import java.util.TimerTask;

 import com.ibm.icu.util.GregorianCalendar;

 public class TaskScheduler {

public static GregorianCalendar result = new GregorianCalendar(2014, 2, 5, 15, 54, 0);
public static GregorianCalendar resulta = new GregorianCalendar(2014, 2, 5, 15, 55, 00);

public static void main(String[] args) throws InterruptedException {


    System.out.println("First thing: " + result.getTime());
    System.out.println("second thing: " + resulta.getTime());


    createNewTimerTask(1);
    createNewTimerTask(21);
    createNewTimerTask(31);
    createNewTimerTask(41);
    createNewTimerTask(51);
    createNewTimerTask(61);
    createNewTimerTask(71);
    createNewTimerTask(81);
    createNewTimerTask(91);
    createNewTimerTask(01);
    createNewTimerTask(111);
    createNewTimerTask(122);
    createNewTimerTask(133);
    createNewTimerTask(144);
    createNewTimerTask(155);
    createNewTimerTask(166);
    createNewTimerTask(177);
}

private static void createNewTimerTask(int id) {
    Timer myTimer = new Timer();
    myTimer.schedule(new Test(id), resulta.getTime());
}
 }



 class Test extends TimerTask {
public static GregorianCalendar result = new GregorianCalendar(2014, 2, 5, 15, 52, 0);
private int id;

Test(int id) {
    this.id = id;
}

@Override
public void run() {
    try {
        Thread.sleep(5000l);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("ID: " + id);
}
}

After the timer task terminates it will be garbage collected. After 100 000 000 events, creating another timer task will run normally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM