1
1
/*
2
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
24
24
*/
25
25
package com .sun .org .apache .xml .internal .utils ;
26
26
27
+ import java .util .concurrent .atomic .AtomicInteger ;
28
+
27
29
/**
28
- * This is a combination of ThreadControllerWrapper's inner class SafeThread
29
- * that was introduced as a fix for CR 6607339
30
- * and sun.misc.ManagedLocalsThread, a thread that has it's thread locals, and
31
- * inheritable thread locals erased on construction. Except the run method,
32
- * it is identical to sun.misc.ManagedLocalsThread.
30
+ * Represents a safe thread that does not inherit thread-locals and runs only
31
+ * once.
33
32
*/
34
33
public class SafeThread extends Thread {
35
-
36
- private static final jdk .internal .misc .Unsafe UNSAFE ;
37
- private static final long THREAD_LOCALS ;
38
- private static final long INHERITABLE_THREAD_LOCALS ;
39
-
40
34
private volatile boolean ran = false ;
41
35
36
+ private static final AtomicInteger threadNumber = new AtomicInteger (1 );
37
+ private static String threadName () {
38
+ return "SafeThread-" + threadNumber .getAndIncrement ();
39
+ }
40
+
42
41
public SafeThread (Runnable target ) {
43
- super (target );
44
- eraseThreadLocals ();
42
+ this (null , target , threadName ());
45
43
}
46
44
47
45
public SafeThread (Runnable target , String name ) {
48
- super (target , name );
49
- eraseThreadLocals ();
46
+ this (null , target , name );
50
47
}
51
48
52
49
public SafeThread (ThreadGroup group , Runnable target , String name ) {
53
- super (group , target , name );
54
- eraseThreadLocals ();
50
+ super (group , target , name , 0 , false );
55
51
}
56
52
57
53
public final void run () {
@@ -69,24 +65,4 @@ public final void run() {
69
65
}
70
66
super .run ();
71
67
}
72
-
73
- /**
74
- * Drops all thread locals (and inherited thread locals).
75
- */
76
- public final void eraseThreadLocals () {
77
- UNSAFE .putReference (this , THREAD_LOCALS , null );
78
- UNSAFE .putReference (this , INHERITABLE_THREAD_LOCALS , null );
79
- }
80
-
81
- static {
82
- UNSAFE = jdk .internal .misc .Unsafe .getUnsafe ();
83
- Class <?> t = Thread .class ;
84
- try {
85
- THREAD_LOCALS = UNSAFE .objectFieldOffset (t .getDeclaredField ("threadLocals" ));
86
- INHERITABLE_THREAD_LOCALS = UNSAFE .objectFieldOffset (t .getDeclaredField ("inheritableThreadLocals" ));
87
- } catch (Exception e ) {
88
- throw new Error (e );
89
- }
90
- }
91
-
92
68
}
0 commit comments