src/java.base/share/classes/java/lang/doc-files/threadPrimitiveDeprecation.html
author smarks
Tue, 12 Jun 2018 13:24:37 -0700
changeset 50530 7e3aa681a484
parent 48942 a6c4b85163c1
child 52116 f0340f0ea249
permissions -rw-r--r--
8204243: remove Thread.destroy() and Thread.stop(Throwable) Reviewed-by: dholmes, alanb, sspitsyn, iris
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 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
    66
<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
    67
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
    68
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
    69
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
    70
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
    71
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
    72
<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
    73
synchronized).</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    74
<p>For example, suppose your applet contains the following
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    75
<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
    76
methods:</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    77
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    78
    private Thread blinker;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    79
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    80
    public void start() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    81
        blinker = new Thread(this);
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    82
        blinker.start();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    83
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    84
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    85
    public void stop() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    86
        blinker.stop();  // UNSAFE!
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    87
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    88
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    89
    public void run() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    90
        while (true) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    91
            try {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    92
                Thread.sleep(interval);
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    93
            } catch (InterruptedException e){
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
            repaint();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    96
        }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    97
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    98
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
    99
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
   100
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
   101
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   102
    private volatile Thread blinker;
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 stop() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   105
        blinker = null;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   106
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   107
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   108
    public void run() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   109
        Thread thisThread = Thread.currentThread();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   110
        while (blinker == thisThread) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   111
            try {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   112
                Thread.sleep(interval);
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   113
            } catch (InterruptedException e){
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   114
            }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   115
            repaint();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   116
        }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   117
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   118
</pre>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   119
<hr>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   120
<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
   121
input)?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   122
<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
   123
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
   124
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
   125
example) can be followed by a call to
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   126
<code>Thread.interrupt</code>, to interrupt the wait:</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   127
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   128
    public void stop() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   129
        Thread moribund = waiter;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   130
        waiter = null;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   131
        moribund.interrupt();
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>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   134
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
   135
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
   136
immediately reasserts the exception. We say <em>reasserts</em>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   137
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
   138
rethrow the exception. If the method that catches the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   139
<code>InterruptedException</code> is not declared to throw this
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   140
(checked) exception, then it should "reinterrupt itself" with the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   141
following incantation:
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
    Thread.currentThread().interrupt();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   144
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   145
This ensures that the Thread will reraise the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   146
<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
   147
<hr>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   148
<h3>What if a thread doesn't respond to
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   149
<code>Thread.interrupt</code>?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   150
<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
   151
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
   152
the socket to cause the thread to return immediately.
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   153
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
   154
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
   155
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
   156
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
   157
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
   158
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
   159
properly.</p>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   160
<hr>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   161
<h3>Why are <code>Thread.suspend</code> and
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   162
<code>Thread.resume</code> deprecated?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   163
<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
   164
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
   165
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
   166
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
   167
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
   168
to calling <code>resume</code>, deadlock results. Such deadlocks
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   169
typically manifest themselves as "frozen" processes.</p>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   170
<hr>
44537
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   171
<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
   172
<code>Thread.resume</code>?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   173
<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
   174
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
   175
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
   176
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
   177
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
   178
<code>Object.notify</code>.</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   179
<p>For example, suppose your applet contains the following
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   180
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
   181
called <code>blinker</code>:</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   182
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   183
    private boolean threadSuspended;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   184
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   185
    Public void mousePressed(MouseEvent e) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   186
        e.consume();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   187
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   188
        if (threadSuspended)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   189
            blinker.resume();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   190
        else
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   191
            blinker.suspend();  // DEADLOCK-PRONE!
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   192
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   193
        threadSuspended = !threadSuspended;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   194
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   195
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   196
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
   197
<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
   198
with:
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   199
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   200
    public synchronized 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
        threadSuspended = !threadSuspended;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   204
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   205
        if (!threadSuspended)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   206
            notify();
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
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   209
and adding the following code to the "run loop":
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
                synchronized(this) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   212
                    while (threadSuspended)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   213
                        wait();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   214
                }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   215
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   216
The <code>wait</code> method throws the
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   217
<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
   218
... 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
   219
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
   220
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
   221
repainted when the thread is "resumed." The resulting
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   222
<code>run</code> method follows:
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
    public void run() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   225
        while (true) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   226
            try {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   227
                Thread.sleep(interval);
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   228
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   229
                synchronized(this) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   230
                    while (threadSuspended)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   231
                        wait();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   232
                }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   233
            } catch (InterruptedException e){
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   234
            }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   235
            repaint();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   236
        }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   237
    }
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
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
   240
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
   241
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
   242
language, and ensures that <code>wait</code> and
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   243
<code>notify</code> are properly serialized. In practical terms,
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   244
this eliminates race conditions that could cause the "suspended"
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   245
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
   246
indefinitely.
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   247
<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
   248
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
   249
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
   250
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
   251
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
   252
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
   253
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   254
                if (threadSuspended) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   255
                    synchronized(this) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   256
                        while (threadSuspended)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   257
                            wait();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   258
                    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   259
                }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   260
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   261
<p>In the absence of explicit synchronization,
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   262
<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
   263
prompt communication of the suspend-request.</p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   264
The resulting <code>run</code> method is:
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   265
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   266
    private volatile boolean threadSuspended;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   267
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   268
    public void run() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   269
        while (true) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   270
            try {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   271
                Thread.sleep(interval);
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   272
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   273
                if (threadSuspended) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   274
                    synchronized(this) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   275
                        while (threadSuspended)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   276
                            wait();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   277
                    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   278
                }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   279
            } catch (InterruptedException e){
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   280
            }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   281
            repaint();
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
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   284
</pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   285
<hr size="3" noshade="noshade" />
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   286
<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
   287
be safely "stopped" or "suspended"?</h3>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   288
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
   289
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
   290
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
   291
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
   292
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
   293
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
   294
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
   295
resulting in erratic behavior.
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   296
<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
   297
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
   298
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
   299
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
   300
<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
   301
<pre>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   302
    public void run() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   303
        Thread thisThread = Thread.currentThread();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   304
        while (blinker == thisThread) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   305
            try {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   306
                Thread.sleep(interval);
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   307
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   308
                synchronized(this) {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   309
                    while (threadSuspended &amp;&amp; blinker==thisThread)
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   310
                        wait();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   311
                }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   312
            } catch (InterruptedException e){
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   313
            }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   314
            repaint();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   315
        }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   316
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   317
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   318
    public synchronized void stop() {
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   319
        blinker = null;
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   320
        notify();
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   321
    }
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   322
</pre>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 44537
diff changeset
   323
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
   324
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
   325
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
   326
won't miss an interrupt due to a race condition.
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   327
<p><!-- Body text ends here --></p>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   328
</body>
afbe031283f7 8178101: Migrate the thread deprecation technote to javadoc doc-files
chegar
parents:
diff changeset
   329
</html>