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

📄 tij315.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
    Inner(String name) {
      t = <font color=#0000ff>new</font> Thread(<font color=#0000ff>this</font>, name);
      t.start();
    }
    <font color=#0000ff>public</font> <font color=#0000ff>void</font> run() {
      <font color=#0000ff>while</font>(<font color=#0000ff>true</font>) {
        System.out.println(<font color=#0000ff>this</font>);
        <font color=#0000ff>if</font>(--countDown == 0) <font color=#0000ff>return</font>;
        <font color=#0000ff>try</font> {
          Thread.sleep(10);
        } <font color=#0000ff>catch</font> (InterruptedException e) {
          <font color=#0000ff>throw</font> <font color=#0000ff>new</font> RuntimeException(e);
        }
      }
    }
    <font color=#0000ff>public</font> String toString() {
      <font color=#0000ff>return</font> t.getName() + <font color=#004488>": "</font> + countDown;
    }
  }
  <font color=#0000ff>public</font> InnerRunnable1(String name) {
    inner = <font color=#0000ff>new</font> Inner(name);
  }
}

<font color=#009900>// Using an anonymous Runnable implementation:</font>
<font color=#0000ff>class</font> InnerRunnable2 {
  <font color=#0000ff>private</font> <font color=#0000ff>int</font> countDown = 5;
  <font color=#0000ff>private</font> Thread t;
  <font color=#0000ff>public</font> InnerRunnable2(String name) {
    t = <font color=#0000ff>new</font> Thread(<font color=#0000ff>new</font> Runnable() {
      <font color=#0000ff>public</font> <font color=#0000ff>void</font> run() {
        <font color=#0000ff>while</font>(<font color=#0000ff>true</font>) {
          System.out.println(<font color=#0000ff>this</font>);
          <font color=#0000ff>if</font>(--countDown == 0) <font color=#0000ff>return</font>;
          <font color=#0000ff>try</font> {
            Thread.sleep(10);
          } <font color=#0000ff>catch</font> (InterruptedException e) {
            <font color=#0000ff>throw</font> <font color=#0000ff>new</font> RuntimeException(e);
          }
        }
      }
      <font color=#0000ff>public</font> String toString() {
        <font color=#0000ff>return</font> Thread.currentThread().getName() +
          <font color=#004488>": "</font> + countDown;
      }
    }, name);
    t.start();
  }
}

<font color=#009900>// A separate method to run some code as a thread:</font>
<font color=#0000ff>class</font> ThreadMethod {
  <font color=#0000ff>private</font> <font color=#0000ff>int</font> countDown = 5;
  <font color=#0000ff>private</font> Thread t;
  <font color=#0000ff>private</font> String name;
  <font color=#0000ff>public</font> ThreadMethod(String name) { <font color=#0000ff>this</font>.name = name; }
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> runThread() {
    <font color=#0000ff>if</font>(t == <font color=#0000ff>null</font>) {
      t = <font color=#0000ff>new</font> Thread(name) {
        <font color=#0000ff>public</font> <font color=#0000ff>void</font> run() {
          <font color=#0000ff>while</font>(<font color=#0000ff>true</font>) {
            System.out.println(<font color=#0000ff>this</font>);
            <font color=#0000ff>if</font>(--countDown == 0) <font color=#0000ff>return</font>;
            <font color=#0000ff>try</font> {
              sleep(10);
            } <font color=#0000ff>catch</font> (InterruptedException e) {
              <font color=#0000ff>throw</font> <font color=#0000ff>new</font> RuntimeException(e);
            }
          }
        }
        <font color=#0000ff>public</font> String toString() {
          <font color=#0000ff>return</font> getName() + <font color=#004488>": "</font> + countDown;
        }
      };
      t.start();
    }
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> ThreadVariations {
  <font color=#0000ff>private</font> <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    <font color=#0000ff>new</font> InnerThread1(<font color=#004488>"InnerThread1"</font>);
    <font color=#0000ff>new</font> InnerThread2(<font color=#004488>"InnerThread2"</font>);
    <font color=#0000ff>new</font> InnerRunnable1(<font color=#004488>"InnerRunnable1"</font>);
    <font color=#0000ff>new</font> InnerRunnable2(<font color=#004488>"InnerRunnable2"</font>);
    <font color=#0000ff>new</font> ThreadMethod(<font color=#004488>"ThreadMethod"</font>).runThread();
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"InnerThread1: 5"</font>,
      <font color=#004488>"InnerThread2: 5"</font>,
      <font color=#004488>"InnerThread2: 4"</font>,
      <font color=#004488>"InnerRunnable1: 5"</font>,
      <font color=#004488>"InnerThread1: 4"</font>,
      <font color=#004488>"InnerRunnable2: 5"</font>,
      <font color=#004488>"ThreadMethod: 5"</font>,
      <font color=#004488>"InnerRunnable1: 4"</font>,
      <font color=#004488>"InnerThread2: 3"</font>,
      <font color=#004488>"InnerRunnable2: 4"</font>,
      <font color=#004488>"ThreadMethod: 4"</font>,
      <font color=#004488>"InnerThread1: 3"</font>,
      <font color=#004488>"InnerRunnable1: 3"</font>,
      <font color=#004488>"ThreadMethod: 3"</font>,
      <font color=#004488>"InnerThread1: 2"</font>,
      <font color=#004488>"InnerThread2: 2"</font>,
      <font color=#004488>"InnerRunnable2: 3"</font>,
      <font color=#004488>"InnerThread2: 1"</font>,
      <font color=#004488>"InnerRunnable2: 2"</font>,
      <font color=#004488>"InnerRunnable1: 2"</font>,
      <font color=#004488>"ThreadMethod: 2"</font>,
      <font color=#004488>"InnerThread1: 1"</font>,
      <font color=#004488>"InnerRunnable1: 1"</font>,
      <font color=#004488>"InnerRunnable2: 1"</font>,
      <font color=#004488>"ThreadMethod: 1"</font>
    }, Test.IGNORE_ORDER + Test.WAIT);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p><b>InnerThread1</b> creates a named inner class that extends <b>Thread</b>, and makes an instance of this inner class inside the constructor. This makes sense if the inner class has special capabilities (new methods) that you need to access in other methods. However, most of the time the reason for creating a thread is only to use the <b>Thread</b> capabilities, so it&#146;s not necessary to create a named inner class. <b>InnerThread2</b> shows the alternative: An anonymous inner subclass of <b>Thread</b> is created inside the constructor and is upcast to a <b>Thread</b> reference <b>t</b>. If other methods of the class need to access <b>t</b>, they can do so through the <b>Thread</b> interface, and they don&#146;t need to know the exact type of the object.<br></p>
<p>The third and fourth classes in the example repeat the first two classes, but they use the <b>Runnable</b> interface rather than the <b>Thread</b> class. This is just to show that <b>Runnable</b> doesn&#146;t buy you anything more in this situation, but is in fact slightly more complicated to code (and to read the code). As a result, my inclination is to use <b>Thread</b> unless I&#146;m somehow compelled to use <b>Runnable</b>.<br></p>
<p>The <b>ThreadMethod</b> class shows the creation of a thread inside a method. You call the method when you&#146;re ready to run the thread, and the method returns after the thread begins. If the thread is only performing an auxiliary operation rather than being fundamental to the class, this is probably a more useful/appropriate approach than starting a thread inside the constructor of the class.<br></p>
<h3>
<a name="_Toc24775844"></a><a name="Heading17484"></a>Creating responsive user
interfaces<br></h3>
<p><a name="Index1554"></a><a name="Index1555"></a><a name="Index1556"></a>As stated earlier, one of the motivations for using threading is to create a responsive user interface. Although we won&#146;t get to <i>graphical </i>user interfaces until Chapter 14, you can see a simple example of a console-based user interface. The following example has two versions: one that gets stuck in a calculation and thus can never read console input, and a second that puts the calculation inside a thread and thus can be performing the calculation <i>and </i>listening for console input.<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c13:ResponsiveUI.java</font>
<font color=#009900>// User interface responsiveness.</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;

<font color=#0000ff>class</font> UnresponsiveUI {
  <font color=#0000ff>private</font> <font color=#0000ff>volatile</font> <font color=#0000ff>double</font> d = 1;
  <font color=#0000ff>public</font> UnresponsiveUI() <font color=#0000ff>throws</font> Exception {
    <font color=#0000ff>while</font>(d &gt; 0)
      d = d + (Math.PI + Math.E) / d;
    System.in.read(); <font color=#009900>// Never gets here</font>
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> ResponsiveUI <font color=#0000ff>extends</font> Thread {
  <font color=#0000ff>private</font> <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <font color=#0

⌨️ 快捷键说明

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