jaulib v1.3.0
Jau Support Library (C++, Java, ..)
InterruptSource.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2015 Gothel Software e.K.
5 * Copyright (c) 2015 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27package org.jau.lang;
28
29/**
30 * Interface exposing {@link java.lang.Thread#interrupt()} source,
31 * intended for {@link java.lang.Thread} specializations.
32 * @since 0.3.0
33 */
34public interface InterruptSource {
35 /**
36 * Returns the source of the last {@link #interrupt()} call.
37 * @param clear if true, issues {@link #clearInterruptSource()}
38 */
39 Throwable getInterruptSource(final boolean clear);
40
41 /**
42 * Returns the count of {@link java.lang.Thread#interrupt()} calls.
43 * @param clear if true, issues {@link #clearInterruptSource()}
44 */
45 int getInterruptCounter(final boolean clear);
46
47 /**
48 * Clears source and count of {@link java.lang.Thread#interrupt()} calls, if any.
49 */
51
52 public static class Util {
53 /**
54 * Casts given {@link java.lang.Thread} to {@link InterruptSource}
55 * if applicable, otherwise returns {@code null}.
56 */
57 public static InterruptSource get(final java.lang.Thread t) {
58 if(t instanceof InterruptSource) {
59 return (InterruptSource)t;
60 } else {
61 return null;
62 }
63 }
64 /**
65 * Casts current {@link java.lang.Thread} to {@link InterruptSource}
66 * if applicable, otherwise returns {@code null}.
67 */
69 return get(java.lang.Thread.currentThread());
70 }
71 }
72
73 /**
74 * {@link java.lang.Thread} specialization implementing {@link InterruptSource}
75 * to track {@link java.lang.Thread#interrupt()} calls.
76 * @since 0.3.0
77 */
78 public static class Thread extends java.lang.Thread implements InterruptSource {
79 volatile Throwable interruptSource = null;
80 volatile int interruptCounter = 0;
81 final Object sync = new Object();
82
83 /**
84 * See {@link Thread#Thread(} for details.
85 */
86 public Thread() {
87 super();
88 }
89 /**
90 * See {@link Thread#Thread(ThreadGroup, Runnable)} for details.
91 * @param tg explicit {@link ThreadGroup}, may be {@code null}
92 * @param target explicit {@link Runnable}, may be {@code null}
93 */
94 public Thread(final ThreadGroup tg, final Runnable target) {
95 super(tg, target);
96 }
97 /**
98 * See {@link Thread#Thread(ThreadGroup, Runnable, String)} for details.
99 * @param tg explicit {@link ThreadGroup}, may be {@code null}
100 * @param target explicit {@link Runnable}, may be {@code null}
101 * @param name explicit name of thread, must not be {@code null}
102 */
103 public Thread(final ThreadGroup tg, final Runnable target, final String name) {
104 super(tg, target, name);
105 }
106
107 /**
108 * Depending on whether {@code name} is null, either
109 * {@link #Thread(ThreadGroup, Runnable, String)} or
110 * {@link #Thread(ThreadGroup, Runnable)} is being utilized.
111 * @param tg explicit {@link ThreadGroup}, may be {@code null}
112 * @param target explicit {@link Runnable}, may be {@code null}
113 * @param name explicit name of thread, may be {@code null}
114 */
115 public static Thread create(final ThreadGroup tg, final Runnable target, final String name) {
116 return null != name ? new Thread(tg, target, name) : new Thread(tg, target);
117 }
118
119 @Override
120 public final Throwable getInterruptSource(final boolean clear) {
121 synchronized(sync) {
122 final Throwable r = interruptSource;
123 if( clear ) {
125 }
126 return r;
127 }
128 }
129 @Override
130 public final int getInterruptCounter(final boolean clear) {
131 synchronized(sync) {
132 final int r = interruptCounter;
133 if( clear ) {
135 }
136 return r;
137 }
138 }
139 @Override
140 public final void clearInterruptSource() {
141 synchronized(sync) {
142 interruptCounter = 0;
143 interruptSource = null;
144 }
145 }
146 @Override
147 public final void interrupt() {
148 synchronized(sync) {
149 interruptCounter++;
150 interruptSource = new Throwable(getName()+".interrupt() #"+interruptCounter);
151 }
152 super.interrupt();
153 }
154 }
155}
java.lang.Thread specialization implementing InterruptSource to track java.lang.Thread#interrupt() ca...
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.
Thread()
See Thread#Thread( 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, final String name)
See Thread#Thread(ThreadGroup, Runnable, String) for details.
final int getInterruptCounter(final boolean clear)
Returns the count of java.lang.Thread#interrupt() calls.
Thread(final ThreadGroup tg, final Runnable target)
See Thread#Thread(ThreadGroup, Runnable) for details.
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...
int getInterruptCounter(final boolean clear)
Returns the count of java.lang.Thread#interrupt() calls.
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.