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 ");}}