⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0155.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 5 页
字号:
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> init() {
    <font color="#009900">// Get parameter "size" from Web page:</font>
    <font color="#0000ff">if</font>(isApplet)
      size = 
        Integer.parseInt(getParameter("size"));
    s = <font color="#0000ff">new</font> Ticker[size];
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; s.length; i++)
      s[i] = <font color="#0000ff">new</font> Ticker(<font color="#0000ff">this</font>);
    start.addActionListener(<font color="#0000ff">new</font> StartL());
    add(start);
  }
  <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) {
      <font color="#0000ff">if</font>(!started) {
        started = <font color="#0000ff">true</font>;
        <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; s.length; i++)
          s[i].start();
      }
    }
  }
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    Counter4 applet = <font color="#0000ff">new</font> Counter4();
    <font color="#009900">// This isn't an applet, so set the flag and</font>
    <font color="#009900">// produce the parameter values from args:</font>
    applet.isApplet = <font color="#0000ff">false</font>;
    applet.size = 
      (args.length == 0 ? 5 :
        Integer.parseInt(args[0]));
    Frame aFrame = <font color="#0000ff">new</font> Frame("Counter4");
    aFrame.addWindowListener(
      <font color="#0000ff">new</font> WindowAdapter() {
        <font color="#0000ff">public</font> <font color="#0000ff">void</font> windowClosing(WindowEvent e) {
          System.exit(0);
        }
      });
    aFrame.add(applet, BorderLayout.CENTER);
    aFrame.setSize(200, applet.size * 50);
    applet.init();
    applet.start();
    aFrame.setVisible(<font color="#0000ff">true</font>);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Ticker</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
contains not only its threading equipment but also the way to control and
display the thread. You can create as many threads as you want without
explicitly creating the windowing components.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Counter4</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
there&#8217;s an array of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Ticker</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>s</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
For maximum flexibility, the size of this array is initialized by reaching out
into the Web page using applet parameters. Here&#8217;s what the size parameter
looks like on the page, embedded inside the applet description:
</FONT><P></DIV>

<font color="#990000"><PRE>&lt;applet code=Counter4 width=600 height=600&gt;
&lt;param name=size value="20"&gt;
&lt;/applet&gt;</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
<A NAME="Index2474"></A><A NAME="Index2475"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>param</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
<A NAME="Index2476"></A><A NAME="Index2477"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>name</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and <A NAME="Index2478"></A><A NAME="Index2479"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>value</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
are all Web-page keywords. 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>name</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is what you&#8217;ll be referring to in your program, and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>value</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can be any string, not just something that resolves to a number.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You&#8217;ll
notice that the determination of the size of the array 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>s</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is done inside 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>init(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and not as part of an inline definition of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>s</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
That is, you 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>cannot</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
say as part of the class definition (outside of any methods):
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#0000ff">int</font> size = Integer.parseInt(getParameter("size"));
Ticker[] s = <font color="#0000ff">new</font> Ticker[size]; </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can compile this, but you&#8217;ll get a strange null-pointer exception at run
time. It works fine if you move the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getParameter(&#160;)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">initialization
inside of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>init(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The <A NAME="Index2480"></A>applet
framework performs the necessary startup to grab the parameters before entering 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>init(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
addition, this code is set up to be either an <A NAME="Index2481"></A>applet
or an <A NAME="Index2482"></A>application.
When it&#8217;s an application the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>size</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
argument is extracted from the command line (or a default value is provided).
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Once
the size of the array is established, new 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Ticker</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects are created; as part of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Ticker</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
constructor the button and text field for each 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Ticker
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
added to the applet.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Pressing
the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>start</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
button means looping through the entire array of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Ticker</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
and calling 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>start(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for each one. Remember, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>start(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
performs necessary thread initialization and then calls 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>run(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for that thread.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ToggleL
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">listener
simply inverts the flag in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Ticker</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and when the associated thread next takes note it can react accordingly.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">One
value of this example is that it allows you to easily create large sets of
independent subtasks and to monitor their behavior. In this case, you&#8217;ll
see that as the number of subtasks gets larger, your machine will probably show
more divergence in the displayed numbers because of the way that the threads
are served.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can also experiment to discover how important the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>sleep(100)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is inside 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Ticker.run(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
If you remove the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>sleep(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
things will work fine until you press a toggle button. Then that particular
thread has a false 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>runFlag</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>run(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is just tied up in a tight infinite loop, which appears difficult to break
during multithreading, so the responsiveness and speed of the program really
bogs down.
</FONT><a name="_Toc375545477"></a><a name="_Toc408018750"></a><P></DIV>
<A NAME="Heading488"></A><H3 ALIGN=LEFT>
Daemon
threads
<P><A NAME="Index2483"></A><A NAME="Index2484"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
&#8220;daemon&#8221; thread is one that is supposed to provide a general
service in the background as long as the program is running, but is not part of
the essence of the program. Thus, when all of the non-daemon threads complete
the program is terminated. Conversely, if there are any non-daemon threads
still running the program doesn&#8217;t terminate. (There is, for instance, a
thread that runs 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;).</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can find out if a thread is a daemon by calling <A NAME="Index2485"></A><A NAME="Index2486"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>isDaemon(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and you can turn the daemonhood of a thread on and off with <A NAME="Index2487"></A><A NAME="Index2488"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>setDaemon(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
If a thread is a daemon, then any threads it creates will automatically be
daemons.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
following example demonstrates daemon threads:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: Daemons.java</font>
<font color="#009900">// Daemonic behavior</font>
<font color="#0000ff">import</font> java.io.*;

<font color="#0000ff">class</font> Daemon <font color="#0000ff">extends</font> Thread {
  <font color="#0000ff">private</font> <font color="#0000ff">static</font> <font color="#0000ff">final</font> <font color="#0000ff">int</font> SIZE = 10;
  <font color="#0000ff">private</font> Thread[] t = <font color="#0000ff">new</font> Thread[SIZE];
  <font color="#0000ff">public</font> Daemon() { 
    setDaemon(<font color="#0000ff">true</font>);
    start();
  }
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> run() {
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; SIZE; i++)
      t[i] = <font color="#0000ff">new</font> DaemonSpawn(i);
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; SIZE; i++)
      System.out.println(
        "t[" + i + "].isDaemon() = " 
        + t[i].isDaemon());
    <font color="#0000ff">while</font>(<font color="#0000ff">true</font>) 
      yield();
  }
}

<font color="#0000ff">class</font> DaemonSpawn <font color="#0000ff">extends</font> Thread {
  <font color="#0000ff">public</font> DaemonSpawn(<font color="#0000ff">int</font> i) {
    System.out.println(
      "DaemonSpawn " + i + " started");
    start();
  }
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> run() {
    <font color="#0000ff">while</font>(<font color="#0000ff">true</font>) 
      yield();
  }
}

<font color="#0000ff">public</font> <font 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -