search
HomeJavaJavaBaseHow to implement delay in java

How to implement delay in java

Dec 26, 2019 pm 04:17 PM
java

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools