Tag Archives: TimerTask

Chapter 3: Animations – Learning Java Bindings for OpenGL (JOGL)

This book and the excerpts on this blogĀ are from 2004. Obviously, the API has changed a bit since then. I provide the older information and book in the hope that it will be useful to some hobbyists.

Purchase Printed Book Learning Java Bindings for OpenGL (JOGL)


Table of Contents: Learning Java Bindings for OpenGL (JOGL)

This book describes JOGL which was approved as JSR 231 and will become the javax.media.opengl package.

-T. Gene Davis



copyright 2004 by Gene Davis of genedavissoftware.com

Chapter 3: Animations

Java Thread Review

Hopefully you’ve used threads before, but I’m going to give you a brief refresher course just in case it is dim in your memory. Threading in pretty much every language comes in two varieties.

One type of threading is to have a timer that acts like an alarm clock. You set it up to go off at some regular interval. In Java, this type of threading can be accomplished with the java.util.Timer class.

Here is a brief example of Timer’s use.

import java.util.*;

 * This sample shows the creation of a Java
 * Timer object. Timers need a task and a
 * set time to wake up and do that task.
public class TimerExample {

    public static void main (String args[]) {

        //our timer
        Timer alarm = new Timer();

        //our task (really a TimerTask object)
        TaskExample te = new TaskExample();

        //we schedule the Timer to wake up
        //and do 'te' ever 2000 milliseconds
        //(every two seconds)
        alarm.scheduleAtFixedRate(te, 0, 2000);

Next we have the actual task to perform.

import java.util.*;

 * Timers need a task to perform. Like we need
 * to go to school or work when our alarm
 * clock goes off, the program needs a task
 * to do when it is woken up.
public class TaskExample extends TimerTask {
     * The task is placed in the run method
     * and performed whenever it is scheduled
     * to do something.
    public void run(){
        System.out.println("Wake up!!!");

The other type of thread is used to do something right away. It is created in Java by using the java.lang.Thread class.

Continue reading Chapter 3: Animations – Learning Java Bindings for OpenGL (JOGL)