Timer Problem December 10, 2013 01:35AM |
Registered: 10 years ago Posts: 8 |
Re: Timer Problem December 10, 2013 12:43PM |
Admin Registered: 15 years ago Posts: 2,094 |
Language: Javapublic class SleepTest { public static void main(final String[] args) throws Exception { for (int i = 0; i < 20; i++) { testSleep(); } } private static void testSleep() throws Exception { final long start = System.nanoTime(); Thread.sleep(1000); final long duration = (System.nanoTime() - start) / 1000000; System.out.println("Slept for " + duration + " ms"); } }
Re: Timer Problem December 14, 2013 05:02AM |
Registered: 10 years ago Posts: 8 |
Re: Timer Problem December 14, 2013 02:46PM |
Admin Registered: 15 years ago Posts: 2,094 |
Language: Javaimport java.util.Timer; import java.util.TimerTask; import java.util.concurrent.CountDownLatch; public class SleepTest2 extends TimerTask { private final CountDownLatch latch = new CountDownLatch(1); private long lastTime; private int i; public static void main(final String[] args) throws Exception { final Timer timer = new Timer(); SleepTest2 test = new SleepTest2(); timer.schedule(test, 0, 1000); test.latch.await(); test = new SleepTest2(); timer.scheduleAtFixedRate(test, 0, 1000); test.latch.await(); timer.cancel(); } @Override public void run() { if (lastTime > 0) { final long duration = (System.nanoTime() - lastTime) / 1000000; System.out.println(i + ": Slept for " + duration + " ms"); } lastTime = System.nanoTime(); if (++i > 10) { cancel(); latch.countDown(); } } }
Re: Timer Problem December 16, 2013 10:21AM |
Registered: 10 years ago Posts: 8 |
Re: Timer Problem December 16, 2013 02:36PM |
Admin Registered: 15 years ago Posts: 2,094 |
Re: Timer Problem December 18, 2013 12:03PM |
Registered: 10 years ago Posts: 8 |
Re: Timer Problem December 19, 2013 12:58AM |
Registered: 10 years ago Posts: 8 |
Re: Timer Problem December 19, 2013 01:53AM |
Registered: 10 years ago Posts: 8 |
Re: Timer Problem December 19, 2013 04:26AM |
Registered: 10 years ago Posts: 8 |