Tag Archives: Thread

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)

Implementing Runnables the Lambda Way

Java 8 introduced Lambda expressions. We’ve been told they would bring simpler easier-to-read programs. We’ve been told that Lambda expressions create multi-core friendly code. Functional programs created with Lambda expressions have fewer bugs, saving development time and maintenance costs. Rumor has it that the release of Java 8 was the moment when the rise of the oceans began to slow and our planet began to heal; it was the moment when we ended all language wars and secured Java’s image as the last, best hope on Earth.

I’m not saying it’s all true. That’s just what I’ve heard.

To decipher rumor from truth, we need to start with some basics. There is a lot of complex, albeit cool, stuff that the experts can do with Lambda expressions. We’ll start with a basic tutorial on how to use a Lambda expression to implement a Runnable.

Continue reading Implementing Runnables the Lambda Way