亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來(lái)到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? chap14.htm

?? Thinking in Java, 2nd edition
?? HTM
?? 第 1 頁(yè) / 共 5 頁(yè)
字號(hào):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source: 
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:TIJ2.rtf
Application Directory:C:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:05/21/2001
Translation Time:10:39:26
Translation Platform:Win32
Number of Output files:23
This File:Chap14.htm
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>14: Multiple Threads</TITLE>
</HEAD>

<BODY  BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
  <a href="http://www.MindView.net">
  <img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>
  <CENTER>
    <FONT FACE="Verdana" size = "-1">
    [ <a href="README.txt">Viewing Hints</a> ]
    [ <a href="RevHist.htm">Revision History</a> ]
    [ <a href="http://www.mindview.net/Books/TIJ/">Book Home Page</a> ]
    [ <a href="http://www.mindview.net/Etc/MailingList.html">Free Newsletter</a> ] <br>
    [ <a href="http://www.mindview.net/Seminars">Seminars</a> ]
    [ <a href="http://www.mindview.net/CDs">Seminars on CD ROM</a> ]
    [ <a href="http://www.mindview.net/Services">Consulting</a> ]
    </FONT>
  <H2><FONT FACE="Verdana">
  Thinking in Java, 2nd edition, Revision 12</FONT></H2>
  <H3><FONT FACE="Verdana">&copy;2000 by Bruce Eckel</FONT></H3>
  
    <FONT FACE="Verdana" size = "-1">
     [ <a href="Chap13.htm">Previous Chapter</a> ] 
    [ <a href="SimpCont.htm">Short TOC</a> ] 
    [ <a href="Contents.htm">Table of Contents</a> ] 
    [ <a href="DocIdx.htm">Index</a> ]
     [ <a href="Chap15.htm">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><A NAME="Chapter_16"></A><A NAME="_Toc375545471"></A><A NAME="_Toc477690734"></A><A NAME="_Toc481064845"></A><A NAME="Heading480"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
14: Multiple Threads</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia" SIZE=4><backtalk:display ID=TIJ3_CHAPTER14_I0>
Objects
provide a way to divide a program into independent sections. Often, you also
need to turn a program into separate, independently running
subtasks.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Each of these independent subtasks is
called a <A NAME="Index1890"></A><A NAME="Index1891"></A><I>thread</I>, and you
program as if each thread runs by itself and has the CPU to itself. Some
underlying mechanism is actually dividing up the CPU time for you, but in
general, you don&#8217;t have to think about it, which makes programming with
multiple threads a much easier task.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER14_I0' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER14_I1>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A <A NAME="Index1892"></A><I>process</I>
is a self-contained running program with its own address space. A
<A NAME="Index1893"></A><I>multitasking</I> operating system is capable of
running more than one process (program) at a time, while making it look like
each one is chugging along on its own, by periodically providing CPU cycles to
each process. A thread is a single sequential flow of control within a process.
A single process can thus have multiple concurrently executing threads.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER14_I1' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER14_I2>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There are many possible uses for
multithreading, but in general, you&#8217;ll have some part of your program tied
to a particular event or resource, and you don&#8217;t want to hang up the rest
of your program because of that. So you create a thread associated with that
event or resource and let it run independently of the main program. A good
example is a &#8220;quit&#8221; button&#8212;you don&#8217;t want to be forced
to poll the quit button in every piece of code you write in your program and yet
you want the quit button to be responsive, as if you <I>were</I> checking it
regularly. In fact, one of the most immediately compelling reasons for
multithreading is to produce a responsive user interface.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER14_I2' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER14_I3>
</FONT><A NAME="_Toc375545472"></A><A NAME="_Toc481064846"></A><BR></P></DIV>
<A NAME="Heading481"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Responsive user interfaces<BR><A NAME="Index1894"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As a starting point, consider a program
that performs some CPU-intensive operation and thus ends up ignoring user input
and being unresponsive. This one, a combined applet/application, will simply
display the result of a running counter:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c14:Counter1.java</font>
<font color=#009900>// A non-responsive user interface.</font>
<font color=#009900>// &lt;applet code=Counter1 width=300 height=100&gt;</font>
<font color=#009900>// &lt;/applet&gt;</font>
<font color=#0000ff>import</font> javax.swing.*;
<font color=#0000ff>import</font> java.awt.event.*;
<font color=#0000ff>import</font> java.awt.*;
<font color=#0000ff>import</font> com.bruceeckel.swing.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Counter1 <font color=#0000ff>extends</font> JApplet {
  <font color=#0000ff>private</font> <font color=#0000ff>int</font> count = 0;
  <font color=#0000ff>private</font> JButton
    start = <font color=#0000ff>new</font> JButton(<font color=#004488>"Start"</font>),
    onOff = <font color=#0000ff>new</font> JButton(<font color=#004488>"Toggle"</font>);
  <font color=#0000ff>private</font> JTextField t = <font color=#0000ff>new</font> JTextField(10);
  <font color=#0000ff>private</font> <font color=#0000ff>boolean</font> runFlag = <font color=#0000ff>true</font>;
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> init() {
    Container cp = getContentPane();
    cp.setLayout(<font color=#0000ff>new</font> FlowLayout());
    cp.add(t);
    start.addActionListener(<font color=#0000ff>new</font> StartL());
    cp.add(start);
    onOff.addActionListener(<font color=#0000ff>new</font> OnOffL());
    cp.add(onOff);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> go() {
    <font color=#0000ff>while</font> (<font color=#0000ff>true</font>) {
      <font color=#0000ff>try</font> {
        Thread.sleep(100);
      } <font color=#0000ff>catch</font>(InterruptedException e) {
        System.err.println(<font color=#004488>"Interrupted"</font>);
      }
      <font color=#0000ff>if</font> (runFlag)
        t.setText(Integer.toString(count++));
    }
  }
  <font color=#0000ff>class</font> StartL <font color=#0000ff>implements</font> ActionListener {
    <font color=#0000ff>public</font> <font color=#0000ff>void</font> actionPerformed(ActionEvent e) {
      go();
    }
  }
  <font color=#0000ff>class</font> OnOffL <font color=#0000ff>implements</font> ActionListener {
    <font color=#0000ff>public</font> <font color=#0000ff>void</font> actionPerformed(ActionEvent e) {
      runFlag = !runFlag;
    }
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Console.run(<font color=#0000ff>new</font> Counter1(), 300, 100);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">At this point, the Swing and applet code
should be reasonably familiar from Chapter 13. The <B>go(&#160;)</B> method is
where the program stays busy: it puts the current value of <B>count</B> into
the<B> JTextField t</B>, then increments <B>count</B>.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER14_I3' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER14_I4>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Part of the infinite loop inside
<B>go(&#160;)</B> is to call <B>sleep(&#160;)</B>.
<A NAME="Index1895"></A><B>sleep(&#160;)</B> must be associated with a
<A NAME="Index1896"></A><B>Thread</B> object, and it turns out that every
application has <I>some</I> thread associated with it. (Indeed, Java is based on
threads and there are always some running along with your application.) So
regardless of whether you&#8217;re explicitly using threads, you can produce the
current thread used by your program with <B>Thread </B>and the static
<B>sleep(&#160;)</B> method.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER14_I4' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER14_I5>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Note that <B>sleep(&#160;)</B> can throw
an <A NAME="Index1897"></A><B>InterruptedException</B>, although throwing such
an exception is considered a hostile way to break from a thread and should be
discouraged. (Once again, exceptions are for exceptional conditions, not normal
flow of control.) Interrupting a sleeping thread is included to support a future
language feature. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER14_I5' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER14_I6>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When the <B>start</B> button is pressed,
<B>go(&#160;)</B> is invoked. On examining <B>go(&#160;)</B>, you might naively
think (as I did) that it should allow multithreading because it goes to sleep.
That is, while the method is asleep, it seems like the CPU could be busy
monitoring other button presses. But it turns out that the real problem is that
<B>go(&#160;)</B> never returns, since it&#8217;s in an infinite loop, and this
means that <B>actionPerformed(&#160;)</B> never returns. Since you&#8217;re
stuck inside <B>actionPerformed(&#160;)</B> for the first keypress, the program
can&#8217;t handle any other events. (To get out, you must somehow kill the
process; the easiest way to do this is to press Control-C in the console window,
if you started it from the console. If you start it via the browser, you have to
kill the browser window.) 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER14_I6' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER14_I7>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The basic problem here is that
<B>go(&#160;)</B> needs to continue performing its operations, and at the same
time it needs to return so that <B>actionPerformed(&#160;)</B> can complete and
the user interface can continue responding to the user. But in a conventional
method like <B>go(&#160;)</B> it cannot continue <I>and</I> at the same time
return control to the rest of the program. This sounds like an impossible thing
to accomplish, as if the CPU must be in two places at once, but this is
precisely the illusion that threading provides. 

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER14_I7' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER14_I8>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The thread model (and its programming
support in Java) is a programming convenience to simplify juggling several
operations at the same time within a single program. With threads, the CPU will
pop around and give each thread some of its time. Each thread has the
consciousness of constantly having the CPU to itself, but the CPU&#8217;s time
is actually sliced between all the threads. The exception to this is if your
program is running on multiple CPUs. But one of the great things about threading
is that you are abstracted away from this layer, so your code does not need to
know whether it is actually running on a single CPU or many. Thus, threads are a
way to create transparently scalable programs.

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
日本精品裸体写真集在线观看| 欧美曰成人黄网| 亚洲欧洲综合另类| 欧美v国产在线一区二区三区| 91日韩精品一区| 精品影视av免费| 亚洲国产视频a| 国产精品久久久久久一区二区三区| 欧美情侣在线播放| 色婷婷久久综合| 福利视频网站一区二区三区| 蜜桃久久久久久| 亚洲chinese男男1069| 国产精品国产三级国产三级人妇 | 宅男噜噜噜66一区二区66| 成人免费毛片aaaaa**| 精品一区二区三区不卡| 亚洲成人1区2区| 亚洲免费av观看| 欧美韩国日本综合| 久久免费电影网| 日韩三级视频在线看| 欧美日韩亚洲综合在线| 色婷婷久久99综合精品jk白丝| 成人美女在线视频| 国产激情一区二区三区桃花岛亚洲| 丝袜美腿高跟呻吟高潮一区| 一区二区三区加勒比av| 中文字幕亚洲综合久久菠萝蜜| 久久久久国色av免费看影院| 日韩欧美中文字幕制服| 91精品国产全国免费观看| 欧美视频在线一区| 欧美日韩久久不卡| 精品污污网站免费看| 欧美午夜精品理论片a级按摩| 色先锋久久av资源部| 在线亚洲高清视频| 91福利精品第一导航| 在线免费视频一区二区| 欧美亚洲日本国产| 欧美三区在线观看| 欧美人成免费网站| 欧美一区二区三区在线观看视频| 欧美精品九九99久久| 日韩一区二区免费在线观看| 日韩欧美综合一区| 亚洲精品在线免费播放| 久久久电影一区二区三区| 国产欧美va欧美不卡在线| 国产精品欧美一区二区三区| 中文字幕中文在线不卡住| 国产精品国产精品国产专区不片| 成人免费一区二区三区在线观看| 亚洲人成网站色在线观看| 亚洲精品欧美二区三区中文字幕| 午夜视频久久久久久| 蜜臀av性久久久久蜜臀aⅴ流畅| 国产一区二区三区久久久| 东方欧美亚洲色图在线| 99精品视频免费在线观看| 在线视频欧美精品| 日韩视频一区二区在线观看| 国产日韩av一区| 亚洲精品日韩综合观看成人91| 三级影片在线观看欧美日韩一区二区| 日本欧美肥老太交大片| 国产成人午夜电影网| 91在线视频播放地址| 91精品国产欧美一区二区| 久久精品人人做| 亚洲欧美视频一区| 久久99久久99小草精品免视看| 国产成人综合网站| 欧美日韩在线三级| 久久一日本道色综合| 亚洲人成伊人成综合网小说| 日本欧美一区二区三区| 成人午夜av电影| 欧美精品久久天天躁| 久久久噜噜噜久久人人看 | 久久久久久亚洲综合影院红桃| 国产精品久久久一本精品| 亚洲国产成人porn| 国产在线不卡一卡二卡三卡四卡| 色999日韩国产欧美一区二区| 日韩视频在线永久播放| 亚洲人成在线观看一区二区| 看电影不卡的网站| 色婷婷久久99综合精品jk白丝| 精品国产亚洲一区二区三区在线观看 | 欧美手机在线视频| 中文字幕乱码一区二区免费| 亚洲va欧美va人人爽午夜| 成人午夜免费电影| 日韩欧美aaaaaa| 一级女性全黄久久生活片免费| 51精品视频一区二区三区| 中文字幕av一区二区三区高 | 国产激情偷乱视频一区二区三区 | 久久综合久久99| 亚洲电影欧美电影有声小说| 国产乱理伦片在线观看夜一区| 欧美性猛交xxxx乱大交退制版| 中文字幕av一区 二区| 蜜臀av亚洲一区中文字幕| 91麻豆免费观看| 中文字幕精品三区| 极品少妇xxxx精品少妇偷拍| 欧美老肥妇做.爰bbww视频| 亚洲欧洲三级电影| 国产精品99久久久久久有的能看| 欧美一区二区视频在线观看2022| 亚洲一二三区在线观看| av亚洲精华国产精华精华| 久久这里都是精品| 免费成人在线播放| 91精品国产一区二区三区蜜臀| 亚洲综合久久久| 色婷婷综合在线| 亚洲日本护士毛茸茸| 成人永久aaa| 亚洲国产成人一区二区三区| 国产精品18久久久久久久网站| 日韩一区二区三区在线观看| 亚洲电影视频在线| 欧美四级电影网| 亚洲国产精品久久久男人的天堂| 日本国产一区二区| 亚洲精品视频免费观看| 91热门视频在线观看| 亚洲精品第1页| 在线看一区二区| 亚洲成人激情自拍| 欧美日韩高清一区二区三区| 亚洲激情成人在线| 在线观看免费一区| 午夜精品在线看| 日韩一级大片在线| 另类中文字幕网| 久久精品人人做人人综合| 国产成人精品亚洲777人妖| 国产欧美日韩另类视频免费观看| 国产大陆亚洲精品国产| 国产精品国产三级国产| 在线免费亚洲电影| 丝袜诱惑制服诱惑色一区在线观看 | 99热国产精品| 亚洲视频一二区| 欧美私人免费视频| 日韩精彩视频在线观看| 欧美一区二区三区电影| 麻豆国产欧美日韩综合精品二区| 日韩美女视频在线| 国产盗摄一区二区三区| 亚洲日本免费电影| 欧美日韩精品一区二区三区蜜桃 | av成人动漫在线观看| 亚洲精品伦理在线| 欧美精品精品一区| 国产精品一区2区| 亚洲老妇xxxxxx| 欧美一区二区视频观看视频| 国产激情一区二区三区桃花岛亚洲| 国产精品国产三级国产普通话三级 | 欧美一区二区三区日韩| 国内精品视频666| 中文字幕一区二区三区视频| 欧美亚洲图片小说| 国产一区二区精品久久91| 亚洲人成亚洲人成在线观看图片| 91超碰这里只有精品国产| 国产麻豆精品95视频| 一二三四区精品视频| 精品久久国产老人久久综合| 91亚洲大成网污www| 青娱乐精品在线视频| 国产精品污网站| 在线播放91灌醉迷j高跟美女| 国产成人久久精品77777最新版本| 亚洲夂夂婷婷色拍ww47| 久久人人爽人人爽| 欧美日韩视频在线第一区 | 亚洲码国产岛国毛片在线| 在线电影欧美成精品| 不卡视频免费播放| 视频一区视频二区中文字幕| 欧美高清在线一区二区| 欧美日韩午夜在线| aaa亚洲精品| 国产在线精品视频| 香蕉成人啪国产精品视频综合网| 欧美极品少妇xxxxⅹ高跟鞋 | 欧美r级电影在线观看| 91啪亚洲精品| 国产精品亚洲一区二区三区妖精| 三级久久三级久久| 一区二区三区不卡视频| 欧美国产激情二区三区| 日韩精品一区二区三区老鸭窝|