interim.html
来自「jsf、swing的官方指南」· HTML 代码 · 共 425 行
HTML
425 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><html> <head> <title>Tasks that Have Interim Results (The Java™ Tutorials > Creating a GUI with JFC/Swing > Concurrency in 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><script type="text/javascript"> function leftBar() { var nameq = 'tutorial_showLeftBar=' var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookieString = cookies[i]; while (cookieString.charAt(0) == ' ') { cookieString = cookieString.substring(1, cookieString.length); } if (cookieString.indexOf(nameq) == 0) { cookieValue = cookieString.substring(nameq.length, cookieString.length); return cookieValue == 'yes'; } } return true; } function showLeft(b) { var contents = document.getElementById("LeftBar"); var main = document.getElementById("MainFlow"); var toggle = document.getElementById("ToggleLeft"); if (b) { contents.className = "LeftBar_shown"; main.className = "MainFlow_indented"; toggle.innerHTML = "Hide the TOC"; document.cookie = 'tutorial_showLeftBar=yes; path=/'; } else { contents.className = "LeftBar_hidden"; main.className = "MainFlow_wide"; toggle.innerHTML = "Show the TOC"; document.cookie = 'tutorial_showLeftBar=no; path=/'; } } function toggleLeft() { showLeft(document.getElementById("LeftBar").className == "LeftBar_hidden"); document.getElementById("ToggleLeft").blur(); } function load() { showLeft(leftBar()); document.getElementById("ToggleLeft").style.display="inline"; } </script> </head><body onload="load()"> <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> <br> <a href="javascript:toggleLeft()" id="ToggleLeft">Hide the TOC</a> </div> </div> </div> </div> </div> </div> <div class=PrintHeaders> <b>Trail:</b> Creating a GUI with JFC/Swing <br><b>Lesson:</b> Concurrency in Swing <br><b>Section:</b> Worker Threads and SwingWorker </div> <div id=LeftBar class=LeftBar_shown> <div id=Contents> <div class="linkLESSON"><a href="index.html">Concurrency in Swing</a></div><div class="linkAHEAD"><a href="initial.html">Initial Threads</a></div><div class="linkAHEAD"><a href="dispatch.html">The Event Dispatch Thread</a></div><div class="linkAHEAD"><a href="worker.html">Worker Threads and SwingWorker</a></div><div class="linkBHEAD"><a href="simple.html">Simple Background Tasks</a></div><div class="nolinkBHEAD">Tasks that Have Interim Results</div><div class="linkBHEAD"><a href="cancel.html">Canceling Background Tasks</a></div><div class="linkBHEAD"><a href="bound.html">Bound Properties and Status Methods</a></div></div> </div> <div id=MainFlow class=MainFlow_indented> <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> > <a href=index.html target=_top>Concurrency in Swing</a> </span> <div class=NavBit> <a target=_top href=simple.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=cancel.html>Next »</a> </div> <div id=PageTitle>Tasks that Have Interim Results</div> <blockquote>It is often useful for a background task to provide interim resultswhile it is still working. The task can do this by invoking<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html#publish(V...)"><code>SwingWorker.publish</code></a>. This method accepts a variable number of arguments. Each argumentmust be of the type specified by <code>SwingWorker</code>'s secondtype parameter.<p>To collect results provided by <code>publish</code>, override<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html#process(java.util.List)"><code>SwingWorker.process</code></a>This method will be invoked from the event dispatch thread. Results from multiple invocations of <code>publish</code> are oftenaccumulated for a single invocation of <code>process</code>.<p>Let's look at the way the<a class="SourceLink" target="_blank" href="examples/Flipper.java"><code><code>Flipper</code></code></a>example uses <code>publish</code> to provide interim results. Thisprogram tests the fairness of<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/util/Random.html"><code>java.util.Random</code></a>by generating a series of random <code>boolean</code> values in abackground task. This is equivalent to flipping a coin; hence thename <code>Flipper</code>. To report its results, the background taskuses an object of type <code>FlipPair</code><blockquote><pre>private static class FlipPair { private final long heads, total; FlipPair(long heads, long total) { this.heads = heads; this.total = total; }}</pre></blockquote>The <code>heads</code> field is the number of times the random valuehas been <code>true</code>; the <code>total</code> field is the totalnumber of random values.<p>The background task is represented by an instance of<code>FlipTask</code>:<blockquote><pre>private class FlipTask extends SwingWorker<Void, FlipPair> {</pre></blockquote>Since the task does not return a final result, it does not matter whatthe first type parameter is; <code>Void</code> is used as aplaceholder. The task invokes <code>publish</code> after each "coinflip":<blockquote><pre>@Overrideprotected Void doInBackground() { long heads = 0; long total = 0; Random random = new Random(); while (!isCancelled()) { total++; if (random.nextBoolean()) { heads++; } publish(new FlipPair(heads, total)); } return null;}</pre></blockquote>(The <code>isCancelled</code> method is discussed in the nextsection.) Because <code>publish</code> is invoked very frequently, alot of <code>FlipPair</code> values will probably be accumulatedbefore <code>process</code> is invoked in the event dispatch thread;<code>process</code> is only intested in the last value reported eachtime, using it to update the GUI:<blockquote><pre>protected void process(List<FlipPair> pairs) { FlipPair pair = pairs.get(pairs.size() - 1); headsText.setText(String.format("%d", pair.heads)); totalText.setText(String.format("%d", pair.total)); devText.setText(String.format("%.10g", ((double) pair.heads)/((double) pair.total) - 0.5));}</pre></blockquote>If <code>Random</code> is fair, the value displayed in<code>devText</code> should get closer and closer to 0 as<code>Flipper</code> runs.<blockquote><hr><strong>Note:</strong> The <code>setText</code> method used in <code>Flipper</code> isactually "thread safe" as defined in its<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setText(java.lang.String)">specification</a>. That means that we could dispense with <code>publish</code> and<code>process</code> and set the text fields directly from theworker thread. We've chosen to ignore this fact in order toprovide a simple demonstration of <code>SwingWorker</code> interimresults.<hr></blockquote> </blockquote> <div class=NavBit> <a target=_top href=simple.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=cancel.html>Next »</a> </div> </div> <div id=Footer><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> Simple Background Tasks <br><b>Next page:</b> Canceling Background Tasks </div> </body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?