Archive

Posts Tagged ‘running’

Passing arguments to a TimerTask in Java

January 17, 2014 No comments

I recently talked about using TimerTask in Java. Today we will pass variables or attributes to that TimerTask. We saw in the last article variable passing to an implicit or inner class, but now we’re creating a subclass and passing arguments through the constructor.

MyTimerTask.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.TimerTask;

class MyTimerTask extends TimerTask
{
    private int times;
    private String result;
    private int tic=0;

    public MyTimerTask (int times, String arg2)
    {
    this.times=times;
    this.result=arg2;
    }

    public String toString()
    {
    return result;
    }

    public void run()
    {
    System.out.println((tic++%2==0)?"TIC":"TOC");
    if (tic%times==0)
        result+="TEN! ";
    }
}

TimerEx.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Timer;
import java.util.TimerTask;

class TimerEx {
    public static void main(String arglist[]) {
    long lonlon=0;
    Timer timer;
    timer = new Timer();

    TimerTask task = new MyTimerTask(10, "Start: ");

    timer.schedule(task, 10, 1000);
    try
        {
        Thread.sleep(11000);
        }
    catch (Exception e)
        {
        }
    System.out.println(task);
    }
}

In this example, we’ve passed two arguments: 10, “Start: “, to the TimerTask (MyTimerTask) constructor, they will be the number of times the task must be launched before writing “TEN! ” on a String. On the other hand, we’ve implemented the method toString in MyTimerTask to write the value directly with System.out.println().

Easy, but, what about passing a callback to execute the System.out.println() ¿?

Let’s write an interface for the Callback (MyTimerCallback), then we will implement MyTimerTask and then we will make TimerEx implement MyTimerCallback, so we can put this function in the main class:

MyTimerCallback.java

1
2
3
4
interface MyTimerCallback
{
    public void timerCallback(MyTimerTask t);
}

MyTimerTask.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.TimerTask;

class MyTimerTask extends TimerTask
{
    private int times;
    private String result="";
    private int tic=0;
    private MyTimerCallback cbClass;

    public MyTimerTask (int times, MyTimerCallback cb)
    {
    this.times=times;
    this.cbClass=cb;
    }

    public String toString()
    {
    return result;
    }

    public void run()
    {
    System.out.println((tic++%2==0)?"TIC":"TOC");
    if (tic%times==0)
        {
        result+="TEN! ";
        cbClass.timerCallback(this);
        }
    }
}

Here, in the constructor, we are asking an object type MyTimerCallback, from which we will call timerCallback().

TimerEx.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Timer;
import java.util.TimerTask;

class TimerEx implements MyTimerCallback
{
    public static void main(String arglist[])
    {
    long lonlon=0;
    Timer timer;
    timer = new Timer();

    TimerTask task = new MyTimerTask(10, new TimerEx());

    timer.schedule(task, 10, 1000);
    }

    public void timerCallback(MyTimerTask t)
    {
    System.out.println(t);
    }
}

From TimerEx, we pass a new TimerEx (as the main method is static and the class is not instanced, we must pass a new object).

Now, everytime 10 tics are executed, we will launch timerCallback(). And of course, we can do it as inner class (not instancing TimerEx, as we can see in the last example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Timer;
import java.util.TimerTask;

class TimerEx implements MyTimerCallback
{
    public static void main(String arglist[])
    {
    long lonlon=0;
    Timer timer;
    timer = new Timer();

    TimerTask task = new MyTimerTask(10, new MyTimerCallback()
        {
        public void timerCallback(MyTimerTask t)
        {
            System.out.println("Inner class: "+t);
        }
        });

    timer.schedule(task, 10, 100);
    }

    public void timerCallback(MyTimerTask t)
    {
    System.out.println(t);
    }
}

[/cc]
Foto: Rob & Stephanie Levy (Flickr) CC-by

Top