Common Java latency methods and java latency Methods

Source: Internet
Author: User

Common Java latency methods and java latency Methods

1. If Thread is used, iu cannot be terminated.

new Thread(new Runnable() {            public void run() {                while (true) {                    test();                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }            }            private void test() {                // TODO Auto-generated method stub            }            public Runnable start() {                // TODO Auto-generated method stub                return null;            }        }.start());

2. Or use the ready-made

Javax. swing. Timer timer = new javax. swing. Timer (500, new ActionListener () {public void actionreceivmed (ActionEvent e) {repaint ();}};

Timer. start ();

3. the following method tests the non-thread latency of java.

Import java. awt. robot; import java. util. date; public class test {public static void main (String [] args) throws Exception {Robot r = new Robot (); System. out. println ("before delay:" + new Date (). toString (); r. delay( 2000); System. out. println ("after delay:" + new Date (). toString ());}}

 

4. Use the following TimeTask class (specified latency) to obtain the source code of the enterprise framework 【]

The sleep () in java is not accurate and timed. TimeTask can be: the following small program:

Import java. util. *; public class test {public static void main (String [] args) {Timer timer = new Timer (); // instantiate the Timer class timer. schedule (new TimerTask () {public void run () {System. out. println ("quit"); this. cancel () ;}}, 5000); // a hundred milliseconds System. out. println ("the program automatically exits after 5 seconds ");}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.