Skip to main content
summaryrefslogtreecommitdiffstats
blob: db0545ff015595854ba58a86d2ff5c35d3422c86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.util.thread;

/**
 * Marker that wraps a Runnable, indicating that it is running in a thread that must not be blocked.
 * <p />
 * Client code can use the thread-local {@link #isNonBlockingThread()} to detect whether they are
 * in the context of a non-blocking thread, and perform different actions if that's the case.
 */
public class NonBlockingThread implements Runnable
{
    private final static ThreadLocal<Boolean> __nonBlockingThread = new ThreadLocal<>();

    /**
     * @return whether the current thread is a thread that must not block.
     */
    public static boolean isNonBlockingThread()
    {
        return Boolean.TRUE.equals(__nonBlockingThread.get());
    }

    private final Runnable delegate;

    public NonBlockingThread(Runnable delegate)
    {
        this.delegate = delegate;
    }

    @Override
    public void run()
    {
        try
        {
            __nonBlockingThread.set(Boolean.TRUE);
            delegate.run();
        }
        finally
        {
            __nonBlockingThread.set(Boolean.FALSE);
        }
    }
}

Back to the top