📄 answers-concurrency.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><html> <head> <title>Answers: Concurrency in Swing (The Java™ Tutorials > Creating a GUI with JFC/Swing > )</title><style type="text/css"> .FigureCaption { margin-left: 1in; margin-right: 1in; font-family: sans-serif; font-size: smaller; text-align: justify; } #TopBar_bl { background: url(../../images/java_bar_bl.gif) 0 100% no-repeat; width: 100%; height: 60px; } #TopBar_br { background: url(../../images/java_bar_br.gif) 100% 100% no-repeat; width: 100%; height: 60px; } #TopBar_tl { background: url(../../images/java_bar_tl.gif) 0 0 no-repeat; width: 100%; height: 60px; } #TopBar_tr { background: url(../../images/java_bar_tr.gif) 100% 0 no-repeat; width: 100%; height: 60px; } #TopBar { background: #35556B url(../../images/java_bar.gif); margin: 10px 10px 0 10px; height:60px; min-width:700px; color: white; font-family: sans-serif; font-weight: bold; } @media print { #BreadCrumbs, #Download { display: none; } } #TopBar_right { line-height: 14px; float: right; padding-top: 2px; padding-right: 30px; text-align: center; } @media print { #TopBar_right { display: none; } } #TopBar_right a { font-size: 12px; margin: 3px; padding: 0; } #TopBar a:visited, #TopBar a:link { color: white; text-decoration: none; } #TopBar a:hover, #TopBar a:active { background-color: white; color: #35556B; } #BreadCrumbs { padding: 4px 5px 0.5em 0; font-family: sans-serif; float: right; } #BreadCrumbs a { color: blue; } #BreadCrumbs a:visited, #BreadCrumbs a:link { text-decoration: none; } #BreadCrumbs a:hover, #BreadCrumbs a:active { text-decoration: underline; } #PageTitle { margin: 0 5px 0.5em 0; color: #E76F00; font-family: sans-serif; font-weight: bold; font-size: 20px; } .LeftBar_shown { width: 13em; float: left; margin-left: 10px; margin-top: 4px; margin-bottom: 2em; } @media print { .LeftBar_shown { display: none; } } .LeftBar_hidden { display: none; } #Footer { padding-top: 10px; padding-left: 10px; margin-right: 10px; } .NavBit { padding: 4px 5px 0.5em 0; font-family: sans-serif; } @media print { .NavBit { display: none; } } #TagNotes { text-align: right; font-size: smaller; font-family: sans-serif; } @media print { #TagNotes a:visited, #TagNotes a:link { color: #35556B; text-decoration: none; } } #Contents a, .NavBit a, #TagNotes a { color: blue } #TagNotes a:visited, #TagNotes a:link, #Contents a:visited, #Contents a:link, .NavBit a:visited, .NavBit a:link { text-decoration: none; } #TagNotes a:hover, #TagNotes a:active, #Contents a:hover, #Contents a:active, .NavBit a:hover, .NavBit a:active { text-decoration: underline; } #Contents { float: left; font-family: sans-serif; } @media print { #Contents { display: none; } } @media screen { div.PrintHeaders { display: none; } } .linkLESSON, .nolinkLESSON { margin-left: 0.5em; text-indent: -0.5em } .linkAHEAD, .nolinkAHEAD, .linkQUESTIONS, .nolinkQUESTIONS { margin-left: 1.5em; text-indent: -0.5em } .linkBHEAD, .nolinkBHEAD { margin-left: 2.5em; text-indent: -0.5em } .linkCHEAD, .nolinkCHEAD { margin-left: 3.5em; text-indent: -0.5em } .nolinkLESSON, .nolinkAHEAD, .nolinkBHEAD, .nolinkCHEAD, .nolinkQUESTIONS { font-weight: bold; color: #E76F00; } .MainFlow_indented { margin-right: 10px; margin-left: 15em; margin-bottom: 2em; } .MainFlow_wide { margin-right: 10px; margin-left: 10px; margin-bottom: 2em; } @media print { .MainFlow_indented, .MainFlow_wide { padding-top: 0; margin-top: 10px; margin-right: 10px; margin-left: 0; } } h3, h4, h5 { color: #E76F00; font-family: sans-serif; } #ToggleLeft { display: none; } /t</style> </head><body> <div id=TopBar> <div id=TopBar_tr> <div id=TopBar_tl> <div id=TopBar_br> <div id=TopBar_bl> <div id=TopBar_right> <a target="_blank" href="http://java.sun.com/javase/6/download.jsp">Download the JDK</a> <br> <a href="../../search.html" target="_blank">Search the Tutorials</a> </div> <div class=PrintHeaders> <b>Trail:</b> Creating a GUI with JFC/Swing </div> </div> </div> </div> </div> </div> <div class=MainFlow_wide> <span id=BreadCrumbs> <a href=../../index.html target=_top>Home Page</a> > <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a> </span> <div class=NavBit> <a href=../TOC.html>« Previous</a> • <a href=../TOC.html>TOC</a> </div> <div id=PageTitle>Answers: Concurrency in Swing</div> <blockquote></blockquote><h3>Questions</h3><BLOCKQUOTE><FONT COLOR="RED">Question 1:</FONT>For each of the following tasks, specify which thread it should beexecuted in and why.<BR><FONT COLOR="GREEN"> Answer 1:</FONT><ul> <li>Initializing the GUI. The event dispatch thread; most interactions with the GUI framework must occur on this thread. <li>Loading a large file. A worker thread. Executing this task on the event dispatch thread would prevent GUI events from being processed, "freezing" the GUI until the task is finished. Executing this task on an initial thread would cause a delay in creating the GUI. <li>Invoking<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#setFont(java.awt.Font)"><code>javax.swing.JComponent.setFont</code></a> to change the font of a component. The event dispatch thread. As with most Swing methods, it is not safe to invoke <code>setFont</code> from any other thread. <li>Invoking<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setText(java.lang.String)"><code>javax.swing.text.JTextComponent.setText</code></a> to change the text of a component. This method is documented as thread-safe, so it can be invoked from any thread.</ul><P><FONT COLOR="RED">Question 2:</FONT> One thread is not the preferred thread for any of the tasksmentioned in the previous question. Name this thread and explain whyits applications are so limited.<BR><FONT COLOR="GREEN"> Answer 2:</FONT> The initial threads launch the first GUI task on the event dispatchthread. After that, a Swing program is primarily driven by GUIevents, which trigger tasks on the event dispatch thread and theworker thread. Usually, the initial threads are left with nothing to do.<P><FONT COLOR="RED">Question 3:</FONT> <code>SwingWorker</code> has two type parameters. Explain howthese type parameters are used, and why it often doesn't matter whatthey are.<BR><FONT COLOR="GREEN"> Answer 3:</FONT> The type parameters specify the type of the final result (alsothe return type of the <code>doInBackground</code> method) and thetype of interim results (also the argument types for<code>publish</code> and <code>process</code>). Many background tasksdo not provide final or interim results.</BLOCKQUOTE><h3>Exercises</h3><BLOCKQUOTE><FONT COLOR="RED">Question 1:</FONT> Modify the<a class="SourceLink" target="_blank" href="../concurrency/examples/Flipper.java"><code><code>Flipper</code></code></a> example so that it pauses 5 seconds between "coin flips." If the user clicks the "Cancel", the coin-flipping loop terminates immediately.<BR><FONT COLOR="GREEN"> Answer 1:</FONT> See the source code for <a class="SourceLink" target="_blank" href="examples/Flipper2.java"><code><code>Flipper2</code></code></a>.The modified program adds a delay in the central<code>doInBackground</code> loop:<blockquote><pre>protected Object doInBackground() { long heads = 0; long total = 0; Random random = new Random(); while (!isCancelled()) { <b>try { Thread.sleep(5000); } catch (InterruptedException e) { //Cancelled! return null; }</b> total++; if (random.nextBoolean()) { heads++; } publish(new FlipPair(heads, total)); } return null;}</pre></blockquote>The <code>try ... catch</code> causes <code>doInBackground</code> toreturn if an interrupt is received while the thread is sleeping.Invoking <code>cancel</code> with an argument of <code>true</code>ensures that an interrupt is sent when the task is cancelled.</BLOCKQUOTE> </blockquote> </div> <div id=Footer> <span class=NavBit> <a href=../TOC.html>« Previous</a> • <a href=../TOC.html>TOC</a> </span><div id=TagNotes> Problems with the examples? Try <a target="_blank" href=../../information/run-examples.html>Compiling and Running the Examples: FAQs</a>. <br> Complaints? Compliments? Suggestions? <a target="_blank" href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give us your feedback</a>.<br><br> <a target="_blank" href="../../information/copyright.html">Copyright</a> 1995-2006 Sun Microsystems, Inc. All rights reserved. <span id=Download></span></div> </div> <div class=PrintHeaders> <b>Previous page:</b> Questions and Exercises: Concurrency in Swing </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -