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

📄 exam01_daan2.htm

📁 清华大学JAVA教程
💻 HTM
字号:
<html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link rel="stylesheet" href="../../css/text.css" type="text/css"></head><body bgcolor="#FFFFFF" text="#000000"><center><table width="95%" border="0" cellspacing="0" cellpadding="0">  <tr>      <td class=text>         <p><b>三、 编程题答案</b></p>        <p>编程第<font color="#0000FF">1</font>题<br>            第一种方式:<br>            <font color="339900">/*Creats two threads that write their names and           sleep. The main thread also writes its <br>            /*name and sleeps. <br>            */</font></p>        <p>  import java.io.*;<br>            public class NameThread extends Thread{<br>             int time; <font color="339900">//time in milliseconds to sleep</font><br>             public NameThread(String n,int t){<br>              super(n);<br>              time=t;<br>             }<br>            public void run(){<br>             for(int i=1;i&lt;=5;i++){<br>              System.out.println(getName()+&quot; &quot;+i);<br>              try{<br>                Thread.sleep(time);<br>              }catch(InterruptedException e){return;}<br>             }<br>            }</p>        <p>  public static void main(String args[]){<br>             NameThread bonnie=new NameThread(&quot;Bonnie&quot;,1000);<br>             bonnie.start();<br>             NameThread clyde=new NameThread(&quot;Clyde&quot;,700);<br>             clyde.start();<br>             for(int i=1;i&lt;=5;i++){<br>              System.out.println(Thread.currentThread().getName()+&quot; &quot;+i);<br>              try{<br>                Thread.sleep(1100);<br>              }catch(InterruptedException e){return;}<br>             }<br>            }<br>            }</p>        <p><br>            第二种方式:<br>            import java.io.*;<br>            public class NameUsingThread implements Runnable{<br>             private int time;<br>             private Thread thread; <font color="339900">//the thread to execute           the run method<br>          </font><br>             public NameUsingThread(String n,int t){<br>              time=t;<br>              thread=new Thread(this,n);<br>              thread.start();<br>             }</p>        <p>    public void run(){<br>              for(int i=1;i&lt;=5;i++){<br>              System.out.println(thread.getName()+&quot; &quot;+i);<br>              try{<br>                Thread.sleep(time);<br>              }catch(InterruptedException e){return;}<br>             }<br>            }<br>          <br>            public static void main(String args[]){<br>             NameUsingThread bonnie=new NameUsingThread(&quot;Bonnie&quot;,1000);<br>             NameUsingThread clyde=new NameUsingThread(&quot;Clyde&quot;,1000);<br>             for(int i=1;i&lt;=5;i++){<br>              System.out.println(Thread.currentThread().getName()+&quot; &quot;+i);<br>              try{<br>                Thread.sleep(1000);<br>              }catch(InterruptedException e){return;}<br>             }<br>            }<br>           }<br>        </p>        <p><br>          编程第<font color="#0000FF">2</font>题<br>            import java.awt.*;<br>            import java.util.*;<br>            import java.awt.event.*;</p>        <p>  public class ThreeClocks extends Panel implements Runnable{<br>             private int timee;<br>             private Thread thd;<br>          <br>             public ThreeClocks(int t){<br>              timee=t;<br>              thd=new Thread(this);<br>              thd.start();<br>             }<br>          <br>             public void run(){<br>              while(thd!=null){<br>               repaint();<br>               try{<br>                 thd.sleep(timee);<br>               }catch(InterruptedException e){}<br>              }<br>             }</p>        <p>   public void paint(Graphics g){<br>             Date now=new Date();<br>            g.drawString(now.getHours()+&quot;:&quot;+now.getMinutes()+&quot;:&quot;+now.getSeconds(),5,10);<br>            }<br>          <br>            public static void main(String args[]){<br>             ThreeClocks Clock1,Clock2,Clock3;<br>             Clock1=new ThreeClocks(1000);<br>             Clock2=new ThreeClocks(5000);<br>             Clock3=new ThreeClocks(10000);<br>             Frame f=new Frame(&quot;Three Clocks running together&quot;);<br>             f.setSize(200,100);<br>             f.setVisible(true);<br>             f.addWindowListener(new WindowAdapter(){<br>            public void windowClosing(WindowEvent e){System.exit(1);}});<br>             f.setLayout(new GridLayout(1,3));<br>             f.add(Clock1);<br>             f.add(Clock2);<br>             f.add(Clock3);<br>            }<br>           }          <!--czp-wenda-daan-->                     </p>        </td>  </tr></table>  </center></body></html>

⌨️ 快捷键说明

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