Home >Java >JavaBase >How to implement delay in java

How to implement delay in java

尚
Original
2019-12-26 16:17:478113browse

How to implement delay in java

How to implement delay in java:

1. Use the Timer class

The Timer class is responsible for the function of planning tasks, that is, starting at the specified time perform a certain task. The Timer class is only used to set scheduled tasks.

The schedule method of the Timer class can execute the program according to the time plan.

 
public static void main(String[] args) {
    Timer timer = new Timer();
    TimerTask timerTask = new MyTimerTask();
    timer.schedule(timerTask, 10000, 10000);    
}

The schedule method needs to pass in an object of type TimerTask, which needs to inherit and implement the run method of the TimerTask class, or implement the run method in the form of an anonymous inner class. The second parameter of schedule is the delay time for the program to execute the run method for the first time, and the third parameter is the time for the delay loop to execute the run method after executing the first run method.

public class MyTimerTask extends TimerTask{
    String userStatus = null;
    String key1 = null;
    String key2 = null;
    Jedis jedis = new Jedis("192.168.16.100",6379);
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        key1 = jedis.get("key1_13681033074");
        key2 = jedis.get("key2_13681033074");
        
        if(key1 != null && key2 == null){
            userStatus = "进入";
        }
 
        if(key1 != null && key2 != null){
            if(key1.equals(key2)){
                userStatus = "驻留";
            }else {
                userStatus = "进入";
            }
        }
 
        if(key1 == null && key2 != null){
            userStatus = "离开";
        }
        
        System.out.println(userStatus);
    }
}

After the run method is implemented, it will be executed according to the time plan set by the schedule. The parameters of the schedule can also be used without cycle time and only delayed execution once. There are also a variety of overloaded schedule methods that can be used according to the actual situation.

2. Using Thread

In Java, sometimes it is necessary to pause the program for a while, which is called delay. For ordinary delays, use the Thread.sleep(int) method, which is very simple. It suspends the current thread for the specified number of milliseconds. For example,

try   
{   
Thread.currentThread().sleep(1000);//毫秒   
}   
catch(Exception e){}

for more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of How to implement delay in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn