Thread In Java Technology

Task Scheduling

Code: reminder.java

import java.util.Timer;
import java.util.TimerTask;

/**
* Simple demo that uses java.util.Timer to schedule a task
* to execute once 5 seconds have passed.
*/

public class Reminder {
    Timer timer;

    public Reminder(int seconds) {
        timer = new Timer();
        timer.schedule(new RemindTask(), seconds*1000);
}

    class RemindTask extends TimerTask {
        public void run() {

            System.out.println("Time's up!");
            timer.cancel(); //Terminate the timer thread
        }
    }

    public static void main(String args[]) {
        new Reminder(5);
        System.out.println("Task scheduled.");
    }
}

Stopping Timer Threads
There are four ways that you can stop a timer:

  • Invoke cancel on the timer. You can do this from anywhere in the program, such as from a timer task抯 run method.
  • Make the timer抯 thread a 揹aemon? by creating the timer like this: new Timer(true). If the only threads left in the program are daemon threads, the program exits.
  • After all the timer抯 scheduled tasks have finished executing, remove all references to the Timer object. Eventually, the timer抯 thread will terminate.
  • Invoke the System.exit method, which makes the entire program (and all its threads) exit.

    Performing a Task Repeatedly
    This can be accomplished by changing a different timer.schedule(); Here are all the schedule APIs that can be used to performe a task repeatedly
  • schedule(TimerTask task, long delay, long period)
  • schedule(TimerTask task, Date time, long period)
  • scheduleAtFixedRate(TimerTask task, long delay, long period)
  • scheduleAtFixedRate(TimerTask task, Date firstTime, long period)


    Implementing the Runnable Interface

    You have now seen two ways to provide the run method.
    • Subclass the Thread class and override the run method. See the SimpleThread class described in the section Subclassing Thread and Overriding run (in the Essential Java Classes trail).
    • Provide a class that implements the Runnable interface and therefore implements the run method. In this case, a Runnable object provides the run method to the thread. See the Clock applet in the preceding section.

    There are good reasons for choosing either of these options over the other. However, for most cases, including that of the Clock applet, if your class must subclass some other class (the most common example being Applet), you should use Runnable.

    import java.awt.Graphics;
    import java.util.*;
    import java.text.DateFormat;
    import java.applet.Applet;
    
    public class Clock extends Applet implements Runnable {
        private Thread clockThread = null;
        public void start() {
            if (clockThread == null) {
                clockThread = new Thread(this, "Clock");
                clockThread.start();
            }
        }
        public void run() {
            Thread myThread = Thread.currentThread();
            while (clockThread == myThread) {
                repaint();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    //the VM doesn抰 want us to sleep anymore,
                    //so get back to work
                }
            }
        }
        public void paint(Graphics g) {
            //get the time and convert it to a date
            Calendar cal = Calendar.getInstance();
            Date date = cal.getTime();
            //format it and display it
            DateFormat dateFormatter =
    				DateFormat.getTimeInstance();
            g.drawString(dateFormatter.format(date), 5, 10);
        }
        //overrides Applet抯 stop method, not Thread抯
        public void stop() {
            clockThread = null;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值