src/java.base/share/classes/java/lang/doc-files/threadPrimitiveDeprecation.html
author mchung
Fri, 23 Feb 2018 12:10:56 -0800
changeset 48942 a6c4b85163c1
parent 47216 71c04702a3d5
child 50530 7e3aa681a484
permissions -rw-r--r--
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit Reviewed-by: dholmes, alanb, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
     1
<!doctype html>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
     2
<!--
48942
a6c4b85163c1 8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents: 47216
diff changeset
     3
 Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
     4
 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
     5
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
     6
 This code is free software; you can redistribute it and/or modify it
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
     7
 under the terms of the GNU General Public License version 2 only, as
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
     8
 published by the Free Software Foundation.  Oracle designates this
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
     9
 particular file as subject to the "Classpath" exception as provided
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    10
 by Oracle in the LICENSE file that accompanied this code.
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    11
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    12
 This code is distributed in the hope that it will be useful, but WITHOUT
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    13
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    14
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    15
 version 2 for more details (a copy is included in the LICENSE file that
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    16
 accompanied this code).
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    17
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    18
 You should have received a copy of the GNU General Public License version
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    19
 2 along with this work; if not, write to the Free Software Foundation,
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    20
 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    21
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    22
 Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    23
 or visit www.oracle.com if you need additional information or have any
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    24
 questions.
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    25
-->
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    26
<html lang="en">
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    27
<head>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    28
  <title>Java Thread Primitive Deprecation</title>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    29
  <link rel="stylesheet" type="text/css" href="../../../stylesheet.css" title="Style">
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    30
</head>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    31
<body>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    32
<h2>Java Thread Primitive Deprecation</h2>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
    33
<hr>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    34
<h3>Why is <code>Thread.stop</code> deprecated?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    35
<p>Because it is inherently unsafe. Stopping a thread causes it to
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    36
unlock all the monitors that it has locked. (The monitors are
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    37
unlocked as the <code>ThreadDeath</code> exception propagates up
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    38
the stack.) If any of the objects previously protected by these
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    39
monitors were in an inconsistent state, other threads may now view
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    40
these objects in an inconsistent state. Such objects are said to be
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    41
<i>damaged</i>. When threads operate on damaged objects, arbitrary
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    42
behavior can result. This behavior may be subtle and difficult to
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    43
detect, or it may be pronounced. Unlike other unchecked exceptions,
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    44
<code>ThreadDeath</code> kills threads silently; thus, the user has
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    45
no warning that his program may be corrupted. The corruption can
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    46
manifest itself at any time after the actual damage occurs, even
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    47
hours or days in the future.</p>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
    48
<hr>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    49
<h3>Couldn't I just catch the <code>ThreadDeath</code> exception
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    50
and fix the damaged object?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    51
<p>In theory, perhaps, but it would <em>vastly</em> complicate the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    52
task of writing correct multithreaded code. The task would be
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    53
nearly insurmountable for two reasons:</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    54
<ol>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    55
<li>A thread can throw a <code>ThreadDeath</code> exception
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    56
<i>almost anywhere</i>. All synchronized methods and blocks would
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    57
have to be studied in great detail, with this in mind.</li>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    58
<li>A thread can throw a second <code>ThreadDeath</code> exception
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    59
while cleaning up from the first (in the <code>catch</code> or
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    60
<code>finally</code> clause). Cleanup would have to be repeated till
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    61
it succeeded. The code to ensure this would be quite complex.</li>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    62
</ol>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    63
In sum, it just isn't practical.
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
    64
<hr>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    65
<h3>What about <code>Thread.stop(Throwable)</code>?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    66
<p>In addition to all of the problems noted above, this method may
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    67
be used to generate exceptions that its target thread is unprepared
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    68
to handle (including checked exceptions that the thread could not
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    69
possibly throw, were it not for this method). For example, the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    70
following method is behaviorally identical to Java's
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    71
<code>throw</code> operation, but circumvents the compiler's
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    72
attempts to guarantee that the calling method has declared all of
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    73
the checked exceptions that it may throw:</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    74
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    75
    static void sneakyThrow(Throwable t) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    76
        Thread.currentThread().stop(t);
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    77
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    78
</pre>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
    79
<hr>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    80
<h3>What should I use instead of <code>Thread.stop</code>?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    81
<p>Most uses of <code>stop</code> should be replaced by code that
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    82
simply modifies some variable to indicate that the target thread
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    83
should stop running. The target thread should check this variable
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    84
regularly, and return from its run method in an orderly fashion if
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    85
the variable indicates that it is to stop running. To ensure prompt
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    86
communication of the stop-request, the variable must be
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
    87
<code>volatile</code> (or access to the variable must be
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    88
synchronized).</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    89
<p>For example, suppose your applet contains the following
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    90
<code>start</code>, <code>stop</code> and <code>run</code>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    91
methods:</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    92
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    93
    private Thread blinker;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    94
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    95
    public void start() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    96
        blinker = new Thread(this);
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    97
        blinker.start();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    98
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    99
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   100
    public void stop() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   101
        blinker.stop();  // UNSAFE!
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   102
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   103
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   104
    public void run() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   105
        while (true) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   106
            try {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   107
                Thread.sleep(interval);
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   108
            } catch (InterruptedException e){
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   109
            }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   110
            repaint();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   111
        }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   112
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   113
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   114
You can avoid the use of <code>Thread.stop</code> by replacing the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   115
applet's <code>stop</code> and <code>run</code> methods with:
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   116
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   117
    private volatile Thread blinker;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   118
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   119
    public void stop() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   120
        blinker = null;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   121
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   122
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   123
    public void run() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   124
        Thread thisThread = Thread.currentThread();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   125
        while (blinker == thisThread) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   126
            try {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   127
                Thread.sleep(interval);
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   128
            } catch (InterruptedException e){
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   129
            }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   130
            repaint();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   131
        }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   132
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   133
</pre>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   134
<hr>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   135
<h3>How do I stop a thread that waits for long periods (e.g., for
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   136
input)?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   137
<p>That's what the <code>Thread.interrupt</code> method is for. The
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   138
same "state based" signaling mechanism shown above can be used, but
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   139
the state change (<code>blinker = null</code>, in the previous
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   140
example) can be followed by a call to
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   141
<code>Thread.interrupt</code>, to interrupt the wait:</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   142
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   143
    public void stop() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   144
        Thread moribund = waiter;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   145
        waiter = null;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   146
        moribund.interrupt();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   147
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   148
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   149
For this technique to work, it's critical that any method that
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   150
catches an interrupt exception and is not prepared to deal with it
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   151
immediately reasserts the exception. We say <em>reasserts</em>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   152
rather than <em>rethrows</em>, because it is not always possible to
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   153
rethrow the exception. If the method that catches the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   154
<code>InterruptedException</code> is not declared to throw this
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   155
(checked) exception, then it should "reinterrupt itself" with the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   156
following incantation:
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   157
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   158
    Thread.currentThread().interrupt();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   159
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   160
This ensures that the Thread will reraise the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   161
<code>InterruptedException</code> as soon as it is able.
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   162
<hr>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   163
<h3>What if a thread doesn't respond to
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   164
<code>Thread.interrupt</code>?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   165
<p>In some cases, you can use application specific tricks. For
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   166
example, if a thread is waiting on a known socket, you can close
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   167
the socket to cause the thread to return immediately.
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   168
Unfortunately, there really isn't any technique that works in
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   169
general. <em>It should be noted that in all situations where a
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   170
waiting thread doesn't respond to <code>Thread.interrupt</code>, it
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   171
wouldn't respond to <code>Thread.stop</code> either.</em> Such
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   172
cases include deliberate denial-of-service attacks, and I/O
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   173
operations for which thread.stop and thread.interrupt do not work
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   174
properly.</p>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   175
<hr>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   176
<h3>Why are <code>Thread.suspend</code> and
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   177
<code>Thread.resume</code> deprecated?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   178
<p><code>Thread.suspend</code> is inherently deadlock-prone. If the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   179
target thread holds a lock on the monitor protecting a critical
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   180
system resource when it is suspended, no thread can access this
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   181
resource until the target thread is resumed. If the thread that
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   182
would resume the target thread attempts to lock this monitor prior
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   183
to calling <code>resume</code>, deadlock results. Such deadlocks
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   184
typically manifest themselves as "frozen" processes.</p>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   185
<hr>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   186
<h3>What should I use instead of <code>Thread.suspend</code> and
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   187
<code>Thread.resume</code>?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   188
<p>As with <code>Thread.stop</code>, the prudent approach is to
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   189
have the "target thread" poll a variable indicating the desired
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   190
state of the thread (active or suspended). When the desired state
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   191
is suspended, the thread waits using <code>Object.wait</code>. When
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   192
the thread is resumed, the target thread is notified using
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   193
<code>Object.notify</code>.</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   194
<p>For example, suppose your applet contains the following
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   195
mousePressed event handler, which toggles the state of a thread
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   196
called <code>blinker</code>:</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   197
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   198
    private boolean threadSuspended;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   199
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   200
    Public void mousePressed(MouseEvent e) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   201
        e.consume();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   202
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   203
        if (threadSuspended)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   204
            blinker.resume();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   205
        else
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   206
            blinker.suspend();  // DEADLOCK-PRONE!
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   207
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   208
        threadSuspended = !threadSuspended;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   209
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   210
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   211
You can avoid the use of <code>Thread.suspend</code> and
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   212
<code>Thread.resume</code> by replacing the event handler above
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   213
with:
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   214
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   215
    public synchronized void mousePressed(MouseEvent e) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   216
        e.consume();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   217
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   218
        threadSuspended = !threadSuspended;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   219
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   220
        if (!threadSuspended)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   221
            notify();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   222
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   223
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   224
and adding the following code to the "run loop":
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   225
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   226
                synchronized(this) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   227
                    while (threadSuspended)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   228
                        wait();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   229
                }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   230
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   231
The <code>wait</code> method throws the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   232
<code>InterruptedException</code>, so it must be inside a <code>try
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   233
... catch</code> clause. It's fine to put it in the same clause as
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   234
the <code>sleep</code>. The check should follow (rather than
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   235
precede) the <code>sleep</code> so the window is immediately
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   236
repainted when the thread is "resumed." The resulting
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   237
<code>run</code> method follows:
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   238
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   239
    public void run() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   240
        while (true) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   241
            try {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   242
                Thread.sleep(interval);
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   243
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   244
                synchronized(this) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   245
                    while (threadSuspended)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   246
                        wait();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   247
                }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   248
            } catch (InterruptedException e){
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   249
            }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   250
            repaint();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   251
        }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   252
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   253
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   254
Note that the <code>notify</code> in the <code>mousePressed</code>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   255
method and the <code>wait</code> in the <code>run</code> method are
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   256
inside <code>synchronized</code> blocks. This is required by the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   257
language, and ensures that <code>wait</code> and
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   258
<code>notify</code> are properly serialized. In practical terms,
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   259
this eliminates race conditions that could cause the "suspended"
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   260
thread to miss a <code>notify</code> and remain suspended
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   261
indefinitely.
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   262
<p>While the cost of synchronization in Java is decreasing as the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   263
platform matures, it will never be free. A simple trick can be used
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   264
to remove the synchronization that we've added to each iteration of
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   265
the "run loop." The synchronized block that was added is replaced
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   266
by a slightly more complex piece of code that enters a synchronized
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   267
block only if the thread has actually been suspended:</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   268
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   269
                if (threadSuspended) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   270
                    synchronized(this) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   271
                        while (threadSuspended)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   272
                            wait();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   273
                    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   274
                }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   275
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   276
<p>In the absence of explicit synchronization,
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   277
<code>threadSuspended</code> must be made <code>volatile</code> to ensure
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   278
prompt communication of the suspend-request.</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   279
The resulting <code>run</code> method is:
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   280
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   281
    private volatile boolean threadSuspended;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   282
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   283
    public void run() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   284
        while (true) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   285
            try {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   286
                Thread.sleep(interval);
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   287
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   288
                if (threadSuspended) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   289
                    synchronized(this) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   290
                        while (threadSuspended)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   291
                            wait();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   292
                    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   293
                }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   294
            } catch (InterruptedException e){
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   295
            }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   296
            repaint();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   297
        }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   298
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   299
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   300
<hr size="3" noshade="noshade" />
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   301
<h3>Can I combine the two techniques to produce a thread that may
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   302
be safely "stopped" or "suspended"?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   303
Yes, it's reasonably straightforward. The one subtlety is that the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   304
target thread may already be suspended at the time that another
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   305
thread tries to stop it. If the <code>stop</code> method merely sets
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   306
the state variable (<code>blinker</code>) to null, the target thread
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   307
will remain suspended (waiting on the monitor), rather than exiting
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   308
gracefully as it should. If the applet is restarted, multiple
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   309
threads could end up waiting on the monitor at the same time,
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   310
resulting in erratic behavior.
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   311
<p>To rectify this situation, the <code>stop</code> method must ensure
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   312
that the target thread resumes immediately if it is suspended. Once
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   313
the target thread resumes, it must recognize immediately that it
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   314
has been stopped, and exit gracefully. Here's how the resulting
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   315
<code>run</code> and <code>stop</code> methods look:</p>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   316
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   317
    public void run() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   318
        Thread thisThread = Thread.currentThread();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   319
        while (blinker == thisThread) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   320
            try {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   321
                Thread.sleep(interval);
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   322
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   323
                synchronized(this) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   324
                    while (threadSuspended &amp;&amp; blinker==thisThread)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   325
                        wait();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   326
                }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   327
            } catch (InterruptedException e){
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   328
            }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   329
            repaint();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   330
        }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   331
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   332
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   333
    public synchronized void stop() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   334
        blinker = null;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   335
        notify();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   336
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   337
</pre>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   338
If the <code>stop</code> method calls <code>Thread.interrupt</code>, as
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   339
described above, it needn't call <code>notify</code> as well, but it
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   340
still must be synchronized. This ensures that the target thread
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   341
won't miss an interrupt due to a race condition.
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   342
<hr>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   343
<h3>What about <code>Thread.destroy</code>?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   344
<code>Thread.destroy</code> was never implemented and has been
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   345
deprecated. If it were implemented, it would be deadlock-prone in
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   346
the manner of <code>Thread.suspend</code>. (In fact, it is roughly
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   347
equivalent to <code>Thread.suspend</code> without the possibility
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   348
of a subsequent <code>Thread.resume</code>.)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   349
<p><!-- Body text ends here --></p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   350
</body>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   351
</html>