GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
InterruptSource.java
Go to the documentation of this file.
1/**
2 * Copyright 2015 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.common.util;
30
31/**
32 * Interface exposing {@link java.lang.Thread#interrupt()} source,
33 * intended for {@link java.lang.Thread} specializations.
34 * @since 2.3.2
35 */
36public interface InterruptSource {
37 /**
38 * Returns the source of the last {@link #interrupt()} call.
39 * @param clear if true, issues {@link #clearInterruptSource()}
40 */
41 Throwable getInterruptSource(final boolean clear);
42
43 /**
44 * Returns the count of {@link java.lang.Thread#interrupt()} calls.
45 * @param clear if true, issues {@link #clearInterruptSource()}
46 */
47 int getInterruptCounter(final boolean clear);
48
49 /**
50 * Clears source and count of {@link java.lang.Thread#interrupt()} calls, if any.
51 */
53
54 public static class Util {
55 /**
56 * Casts given {@link java.lang.Thread} to {@link InterruptSource}
57 * if applicable, otherwise returns {@code null}.
58 */
59 public static InterruptSource get(final java.lang.Thread t) {
60 if(t instanceof InterruptSource) {
61 return (InterruptSource)t;
62 } else {
63 return null;
64 }
65 }
66 /**
67 * Casts current {@link java.lang.Thread} to {@link InterruptSource}
68 * if applicable, otherwise returns {@code null}.
69 */
71 return get(java.lang.Thread.currentThread());
72 }
73 }
74
75 /**
76 * {@link java.lang.Thread} specialization implementing {@link InterruptSource}
77 * to track {@link java.lang.Thread#interrupt()} calls.
78 * @since 2.3.2
79 */
80 public static class Thread extends java.lang.Thread implements InterruptSource {
81 volatile Throwable interruptSource = null;
82 volatile int interruptCounter = 0;
83 final Object sync = new Object();
84
85 /**
86 * See {@link Thread#Thread(} for details.
87 */
88 public Thread() {
89 super();
90 }
91 /**
92 * See {@link Thread#Thread(Runnable)} for details.
93 * @param target explicit {@link Runnable}, may be {@code null}
94 */
95 public Thread(final Runnable target) {
96 super(target);
97 }
98 /**
99 * See {@link Thread#Thread(ThreadGroup, Runnable)} for details.
100 * @param tg explicit {@link ThreadGroup}, may be {@code null}
101 * @param target explicit {@link Runnable}, may be {@code null}
102 */
103 public Thread(final ThreadGroup tg, final Runnable target) {
104 super(tg, target);
105 }
106 /**
107 * See {@link Thread#Thread(ThreadGroup, Runnable, String)} for details.
108 * @param tg explicit {@link ThreadGroup}, may be {@code null}
109 * @param target explicit {@link Runnable}, may be {@code null}
110 * @param name explicit name of thread, must not be {@code null}
111 */
112 public Thread(final ThreadGroup tg, final Runnable target, final String name) {
113 super(tg, target, name);
114 }
115
116 /**
117 * Depending on whether {@code name} is null, either
118 * {@link #Thread(ThreadGroup, Runnable, String)} or
119 * {@link #Thread(ThreadGroup, Runnable)} is being utilized.
120 * @param tg explicit {@link ThreadGroup}, may be {@code null}
121 * @param target explicit {@link Runnable}, may be {@code null}
122 * @param name explicit name of thread, may be {@code null}
123 */
124 public static Thread create(final ThreadGroup tg, final Runnable target, final String name) {
125 return null != name ? new Thread(tg, target, name) : new Thread(tg, target);
126 }
127
128 @Override
129 public final Throwable getInterruptSource(final boolean clear) {
130 synchronized(sync) {
131 final Throwable r = interruptSource;
132 if( clear ) {
134 }
135 return r;
136 }
137 }
138 @Override
139 public final int getInterruptCounter(final boolean clear) {
140 synchronized(sync) {
141 final int r = interruptCounter;
142 if( clear ) {
144 }
145 return r;
146 }
147 }
148 @Override
149 public final void clearInterruptSource() {
150 synchronized(sync) {
151 interruptCounter = 0;
152 interruptSource = null;
153 }
154 }
155 @Override
156 public final void interrupt() {
157 synchronized(sync) {
158 interruptCounter++;
159 interruptSource = new Throwable(getName()+".interrupt() #"+interruptCounter);
160 }
161 super.interrupt();
162 }
163 }
164}
java.lang.Thread specialization implementing InterruptSource to track java.lang.Thread#interrupt() ca...
final int getInterruptCounter(final boolean clear)
Returns the count of java.lang.Thread#interrupt() calls.
Thread(final Runnable target)
See Thread#Thread(Runnable) for details.
Thread(final ThreadGroup tg, final Runnable target, final String name)
See Thread#Thread(ThreadGroup, Runnable, String) for details.
static Thread create(final ThreadGroup tg, final Runnable target, final String name)
Depending on whether name is null, either Thread(ThreadGroup, Runnable, String) or Thread(ThreadGroup...
Thread(final ThreadGroup tg, final Runnable target)
See Thread#Thread(ThreadGroup, Runnable) for details.
Thread()
See Thread#Thread( for details.
final void clearInterruptSource()
Clears source and count of java.lang.Thread#interrupt() calls, if any.
final Throwable getInterruptSource(final boolean clear)
Returns the source of the last interrupt() call.
static InterruptSource currentThread()
Casts current java.lang.Thread to InterruptSource if applicable, otherwise returns null.
Interface exposing java.lang.Thread#interrupt() source, intended for java.lang.Thread specializations...
void clearInterruptSource()
Clears source and count of java.lang.Thread#interrupt() calls, if any.
Throwable getInterruptSource(final boolean clear)
Returns the source of the last interrupt() call.
int getInterruptCounter(final boolean clear)
Returns the count of java.lang.Thread#interrupt() calls.