Timer Schedule a task that executes once every second : Timer « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Development Class » TimerScreenshots 
Timer Schedule a task that executes once every second
Timer Schedule a task that executes once every second
     
/* From http://java.sun.com/docs/books/tutorial/index.html */
/*
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 *
 * -Redistribution in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Schedule a task that executes once every second.
 */

public class AnnoyingBeep {
  Toolkit toolkit;

  Timer timer;

  public AnnoyingBeep() {
    toolkit = Toolkit.getDefaultToolkit();
    timer = new Timer();
    timer.schedule(new RemindTask()0//initial delay
        1000)//subsequent rate
  }

  class RemindTask extends TimerTask {
    int numWarningBeeps = 3;

    public void run() {
      if (numWarningBeeps > 0) {
        toolkit.beep();
        System.out.println("Beep!");
        numWarningBeeps--;
      else {
        toolkit.beep();
        System.out.println("Time's up!");
        //timer.cancel(); //Not necessary because we call System.exit
        System.exit(0)//Stops the AWT thread (and everything else)
      }
    }
  }

  public static void main(String args[]) {
    System.out.println("About to schedule task.");
    new AnnoyingBeep();
    System.out.println("Task scheduled.");
  }
}

           
         
    
    
    
    
  
Related examples in the same category
1.Timer Skipping BeepTimer Skipping Beep
2.Use java.util.Timer to schedule a task to execute once 5 seconds have passedUse java.util.Timer to schedule a task to execute once 5 seconds have passed
3.Timer utilities
4.Timer and TimerTask Classes
5.Pause and start a timer task
6.Create a Timer object
7.Swing also provide a Timer class. A Timer object will send an ActionEvent to the registered ActionListener.
8.Schedule a task by using Timer and TimerTask.
9.Scheduling a Timer Task to Run Repeatedly
10.Create a scheduled task using timer
11.extends TimerTask to create your own task
12.A simple implementation of the Java 1.3 java.util.Timer API
13.Scheduling a Timer Task to Run at a Certain Time
14.Class encapsulating timer functionality
15.Timeout Observer
16.A pool of objects that should only be used by one thread at a time
17.A class that allows a programmer to determine the amount of time spend doing certain routines
18.All times in this Timer object are in milliseconds. The timer starts from the moment of it's creation.
19.Thread Timer
20.Cancel Aware Timer
21.HRTimer is a simple system-wide timer facility using a singleton Timer, with additional instrumentation.
22.Used for timing events with millisecond precision.
23.A class to wait until a condition becomes true.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.