Search in sources :

Example 71 with Timer

use of java.util.Timer in project robovm by robovm.

the class TimerTest method test_ConstructorS.

/**
     * java.util.Timer#Timer(String)
     */
public void test_ConstructorS() throws Exception {
    Timer t = null;
    try {
        // Ensure a task is run
        t = new Timer("test_ConstructorSThread");
        TimerTestTask testTask = new TimerTestTask();
        t.schedule(testTask, 200);
        awaitRun(testTask);
        t.cancel();
    } finally {
        if (t != null)
            t.cancel();
    }
    try {
        new Timer(null);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : Timer(java.util.Timer)

Example 72 with Timer

use of java.util.Timer in project robovm by robovm.

the class TimerTest method test_scheduleLjava_util_TimerTaskJJ.

/**
     * java.util.Timer#schedule(java.util.TimerTask, long, long)
     */
public void test_scheduleLjava_util_TimerTaskJJ() throws Exception {
    Timer t = null;
    try {
        // Ensure a Timer throws an IllegalStateException after cancelled
        t = new Timer();
        TimerTestTask testTask = new TimerTestTask();
        t.cancel();
        try {
            t.schedule(testTask, 100, 100);
            fail("Scheduling a task after Timer.cancel() should throw exception");
        } catch (IllegalStateException expected) {
        }
        // Ensure a Timer throws an IllegalStateException if task already
        // cancelled
        t = new Timer();
        testTask = new TimerTestTask();
        testTask.cancel();
        try {
            t.schedule(testTask, 100, 100);
            fail("Scheduling a task after cancelling it should throw exception");
        } catch (IllegalStateException expected) {
        }
        t.cancel();
        // Ensure a Timer throws an IllegalArgumentException if delay is
        // negative
        t = new Timer();
        testTask = new TimerTestTask();
        try {
            t.schedule(testTask, -100, 100);
            fail("Scheduling a task with negative delay should throw IllegalArgumentException");
        } catch (IllegalArgumentException expected) {
        }
        t.cancel();
        // Ensure a Timer throws an IllegalArgumentException if period is
        // negative
        t = new Timer();
        testTask = new TimerTestTask();
        try {
            t.schedule(testTask, 100, -100);
            fail("Scheduling a task with negative period should throw IllegalArgumentException");
        } catch (IllegalArgumentException expected) {
        }
        t.cancel();
        // Ensure a Timer throws an IllegalArgumentException if period is
        // zero
        t = new Timer();
        testTask = new TimerTestTask();
        try {
            t.schedule(testTask, 100, 0);
            fail("Scheduling a task with 0 period should throw IllegalArgumentException");
        } catch (IllegalArgumentException expected) {
        }
        t.cancel();
        // Ensure a Timer throws a NullPointerException if the task is null
        t = new Timer();
        try {
            t.schedule(null, 10, 10);
            fail("Scheduling a null task should throw NullPointerException");
        } catch (NullPointerException expected) {
        }
        t.cancel();
        // Ensure proper sequence of exceptions
        t = new Timer();
        try {
            t.schedule(null, -10, -10);
            fail("Scheduling a null task with negative delays should throw IllegalArgumentException first");
        } catch (IllegalArgumentException expected) {
        }
        t.cancel();
        // Ensure a task is run at least twice
        t = new Timer();
        testTask = new TimerTestTask();
        t.schedule(testTask, 100, 100);
        Thread.sleep(400);
        assertTrue("TimerTask.run() method should have been called at least twice (" + testTask.wasRun() + ")", testTask.wasRun() >= 2);
        t.cancel();
        // Ensure multiple tasks are run
        t = new Timer();
        testTask = new TimerTestTask();
        testTask.incrementCount(true);
        // at least 9 times
        t.schedule(testTask, 100, 100);
        testTask = new TimerTestTask();
        testTask.incrementCount(true);
        // at least 7 times
        t.schedule(testTask, 200, 100);
        testTask = new TimerTestTask();
        testTask.incrementCount(true);
        // at least 4 times
        t.schedule(testTask, 300, 200);
        testTask = new TimerTestTask();
        testTask.incrementCount(true);
        // at least 4 times
        t.schedule(testTask, 100, 200);
        // Allowed more room for error
        Thread.sleep(1200);
        assertTrue("Multiple tasks should have incremented counter 24 times not " + timerCounter, timerCounter >= 24);
        t.cancel();
    } finally {
        if (t != null)
            t.cancel();
    }
}
Also used : Timer(java.util.Timer)

Example 73 with Timer

use of java.util.Timer in project spring-boot by spring-projects.

the class SnakeTimer method startTimer.

public static void startTimer() {
    gameTimer = new Timer(SnakeTimer.class.getSimpleName() + " Timer");
    gameTimer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            try {
                tick();
            } catch (Throwable ex) {
                log.error("Caught to prevent timer from shutting down", ex);
            }
        }
    }, TICK_DELAY, TICK_DELAY);
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask)

Example 74 with Timer

use of java.util.Timer in project spring-boot by spring-projects.

the class SnakeTimer method startTimer.

public static void startTimer() {
    gameTimer = new Timer(SnakeTimer.class.getSimpleName() + " Timer");
    gameTimer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            try {
                tick();
            } catch (Throwable ex) {
                log.error("Caught to prevent timer from shutting down", ex);
            }
        }
    }, TICK_DELAY, TICK_DELAY);
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask)

Example 75 with Timer

use of java.util.Timer in project socket.io-client-java by socketio.

the class ConnectionTest method reconnectByDefault.

@Test(timeout = TIMEOUT)
public void reconnectByDefault() throws URISyntaxException, InterruptedException {
    final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
    socket = client();
    socket.io().on(Manager.EVENT_RECONNECT, new Emitter.Listener() {

        @Override
        public void call(Object... objects) {
            socket.close();
            values.offer("done");
        }
    });
    socket.open();
    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {
            socket.io().engine.close();
        }
    }, 500);
    values.take();
}
Also used : Emitter(io.socket.emitter.Emitter) Timer(java.util.Timer) TimerTask(java.util.TimerTask) JSONObject(org.json.JSONObject) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Aggregations

Timer (java.util.Timer)448 TimerTask (java.util.TimerTask)262 IOException (java.io.IOException)33 Date (java.util.Date)22 Test (org.junit.Test)15 File (java.io.File)11 ArrayList (java.util.ArrayList)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 JobExecutor (org.activiti.engine.impl.jobexecutor.JobExecutor)9 Intent (android.content.Intent)8 Location (android.location.Location)8 LocationListener (android.location.LocationListener)8 Bundle (android.os.Bundle)8 ActivitiException (org.activiti.engine.ActivitiException)8 View (android.view.View)7 InputMethodManager (android.view.inputmethod.InputMethodManager)7 ImageView (android.widget.ImageView)7 BufferedReader (java.io.BufferedReader)7 InputStreamReader (java.io.InputStreamReader)7