1

Currently my project has no timing and is satisfactory but it would be a lot cleaner if I had timing. I would like to have it so after I declared whatever needs to be I can do something like:

if (timer == 2000) { System.out.println("2 seconds have passed since "timer" was declared!"); } 

I haven't seen that much that went over this. Sorry if I'm missing something. I'm just looking for the best way to do this.

EDIT
I've been asked for more detail on why I need this so... btw this is for a zombie game

while(!Display.isCloseRequested()){ if (whatever == 2 seconds){ //I obviously know a don't right the word seconds next the number 2 for(int z = 0; z < 2; z++){ SpawnaZombieAt(leftcorner,20,speed); } } } 

if I'm not clear enough I am trying to start some sort of timer. then for every 2 seconds or so 2 more zombies will spawn

5
  • 1
    Why don't you provide more specifics on what you hope to accomplish by doing something like this? Use edit under your question to add more details to it. Commented May 13, 2012 at 20:26
  • The best way to provide timed events in Java depends on the exact requirements. The core API offers a number of timer classes of varying constraints and accuracy. There are also a number of 3rd party APIs. Be more specific in your requirements. Commented May 13, 2012 at 20:28
  • 2
    Also, the chances of anything timing-related equaling precisely 2000 milliseconds are low. Commented May 13, 2012 at 20:30
  • Are you trying to add measurement/instrumentation to your project (e.g., to measure performance) OR are you trying to schedule activities to occur a specific time? If the latter, what resolution are you looking for (subsecond? or "run this hourly, give or take?" or something in the middle?) Commented May 13, 2012 at 20:49
  • This is in a game and it is so every 2 seconds there will be a zombie that spawns. Commented May 13, 2012 at 22:11

2 Answers 2

2

Are you looking for System.currentTimeMillis() or System.nanoTime()? The first is a low precision counter that corresponds to UTC, while the latter is a high precision timer that can be used to determine elapsed time.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to launch a specific action after a specific delay, you can use a ScheduledExecutorService - for example:

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); scheduler.schedule(new Runnable() { public void run() { System.out.println("2 seconds have passed since this action was scheduled!"); } }, 2, TimeUnit.SECONDS); 

And it will print your sentence after 2 seconds.

If you are after a stopwatch, the answers to this question propose several alternatives.

EDIT

Following your edit, the ScheduledExecutorService has a scheduleAtFixedRate method that would enable you to run a specific action every 2 seconds for example.

1 Comment

I'm looking more to make this with the System.currentTimeMillis thing and some variables though

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.