Java Code Examples for java.lang.Thread.State#TIMED_WAITING

The following examples show how to use java.lang.Thread.State#TIMED_WAITING . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OverviewController.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** taken from sun.misc.VM
 * 
 * Returns Thread.State for the given threadStatus
 */
private static Thread.State toThreadState(int threadStatus) {
    if ((threadStatus & JVMTI_THREAD_STATE_RUNNABLE) != 0) {
        return State.RUNNABLE;
    } else if ((threadStatus & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) != 0) {
        return State.BLOCKED;
    } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_INDEFINITELY) != 0) {
        return State.WAITING;
    } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT) != 0) {
        return State.TIMED_WAITING;
    } else if ((threadStatus & JVMTI_THREAD_STATE_TERMINATED) != 0) {
        return State.TERMINATED;
    } else if ((threadStatus & JVMTI_THREAD_STATE_ALIVE) == 0) {
        return State.NEW;
    } else {
        return State.RUNNABLE;
    }
}
 
Example 2
Source File: OverviewController.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
/** taken from sun.misc.VM
 * 
 * Returns Thread.State for the given threadStatus
 */
private static Thread.State toThreadState(int threadStatus) {
    if ((threadStatus & JVMTI_THREAD_STATE_RUNNABLE) != 0) {
        return State.RUNNABLE;
    } else if ((threadStatus & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) != 0) {
        return State.BLOCKED;
    } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_INDEFINITELY) != 0) {
        return State.WAITING;
    } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT) != 0) {
        return State.TIMED_WAITING;
    } else if ((threadStatus & JVMTI_THREAD_STATE_TERMINATED) != 0) {
        return State.TERMINATED;
    } else if ((threadStatus & JVMTI_THREAD_STATE_ALIVE) == 0) {
        return State.NEW;
    } else {
        return State.RUNNABLE;
    }
}
 
Example 3
Source File: GameReplayer.java    From Slyther with MIT License 5 votes vote down vote up
public GameReplayer(File file) {
    this.file = file;
    server = new WebSocketServer(new InetSocketAddress(8004)) {
        @Override
        public void onOpen(WebSocket conn, ClientHandshake handshake) {
            if (waitingForOpen) {
                thread = new Thread(GameReplayer.this, "Replayer");
                thread.start();   
                waitingForOpen = false;
            } else {
                Log.warn("Connection was attempted to be made during playback: {}", conn.getRemoteSocketAddress());
                conn.close();   
            }
        }

        @Override
        public void onClose(WebSocket conn, int code, String reason, boolean remote) {
            waitingForClose = true;
            // Interrupt a long message delay
            while (waitingForClose) {
                if (thread.getState() == State.TIMED_WAITING) {
                    thread.interrupt();
                }
            }
        }

        @Override
        public void onMessage(WebSocket conn, String message) {}

        @Override
        public void onError(WebSocket conn, Exception ex) {
            if (ex instanceof BindException) {
                Log.catching(ex);
            }
        }
    };
    server.start();
}
 
Example 4
Source File: QueueSanityTestMpscBlockingConsumerArrayExtended.java    From JCTools with Apache License 2.0 5 votes vote down vote up
private void testTakeBlocksAndIsInterrupted(boolean withTimeout) throws Exception
{
    final AtomicBoolean wasInterrupted = new AtomicBoolean();
    final AtomicBoolean interruptedStatusAfter = new AtomicBoolean();
    final MpscBlockingConsumerArrayQueue<Integer> q = new MpscBlockingConsumerArrayQueue<>(1024);
    Thread consumer = new Thread(() -> {
        try
        {
            Integer take = withTimeout ? q.poll(1L, DAYS) : q.take();
        }
        catch (InterruptedException e)
        {
            wasInterrupted.set(true);
        }
        interruptedStatusAfter.set(Thread.currentThread().isInterrupted());
    });
    consumer.setDaemon(true);
    consumer.start();
    State waitState = withTimeout ? State.TIMED_WAITING : State.WAITING;
    while(consumer.getState() != waitState)
    {
        Thread.yield();
    }
    // If we got here -> thread got to the waiting state -> parked
    consumer.interrupt();
    consumer.join();
    assertTrue(wasInterrupted.get());
    assertFalse(interruptedStatusAfter.get());

    // Queue should remain in original state (empty)
    assertNull(q.poll());
}
 
Example 5
Source File: QueueSanityTestMpscBlockingConsumerArrayExtended.java    From JCTools with Apache License 2.0 4 votes vote down vote up
private void testTakeSomeElementsThenBlocksAndIsInterrupted(boolean withTimeout) throws Exception
{
    Val v = new Val();
    final AtomicBoolean wasInterrupted = new AtomicBoolean();
    final MpscBlockingConsumerArrayQueue<Integer> q = new MpscBlockingConsumerArrayQueue<>(1024);
    Thread consumer = new Thread(() -> {
        try
        {
            while (true)
            {
                Integer take = withTimeout ? q.poll(1L, DAYS) : q.take();
                assertNotNull(take); // take never returns null
                assertEquals(take.intValue(), v.value);
                v.value++;
            }
        }
        catch (InterruptedException e)
        {
            wasInterrupted.set(true);
        }
    });
    consumer.setDaemon(true);
    consumer.start();
    State waitState = withTimeout ? State.TIMED_WAITING : State.WAITING;
    while(consumer.getState() != waitState)
    {
        Thread.yield();
    }
    // If we got here -> thread got to the waiting state -> parked
    int someElements = ThreadLocalRandom.current().nextInt(10000);
    for (int i=0;i < someElements; i++)
        while (!q.offer(i));

    while(!q.isEmpty())
    {
        Thread.yield();
    }
    // Eventually queue is drained

    while(consumer.getState() != waitState)
    {
        Thread.yield();
    }
    // If we got here -> thread got to the waiting state -> parked

    consumer.interrupt();
    consumer.join();
    assertTrue(wasInterrupted.get());
    assertEquals(someElements, v.value);
}