資源簡介
編寫三個(gè)線程:各線程分別顯示各自的運(yùn)行時(shí)間,第一個(gè)線程每隔1秒鐘運(yùn)行一次,第二個(gè)每隔5秒鐘運(yùn)行一次,第三個(gè)線程每隔10秒鐘運(yùn)行一次。

代碼片段和文件信息
package?clock;
import?java.awt.*;
import?javax.swing.*;
import?java.util.*;
import?java.text.SimpleDateFormat;
public?class?Clock?extends?Japplet?implements?Runnable
{
????Thread?clockThread1;
????Thread?clockThread2;
????Thread?clockThread3;
????boolean?f1f2f3;
????SimpleDateFormat?formatter;
????Date?currentDate1currentDate2currentDate3;
????String?lastdate1lastdate2lastdate3;
????public?void?init()
????{
????????super.setBackground(Color.LIGHT_GRAY);
????????formatter=new?SimpleDateFormat(“hh:mm:ss“);
????????currentDate1=new?Date();
????????lastdate3=lastdate2=lastdate1=formatter.format(currentDate1);
????????clockThread1=new?Thread(this“Clock-thread1“);
????????clockThread2=new?Thread(this“Clock-thread2“);
????????clockThread3=new?Thread(this“Clock-thread3“);
????????clockThread1.start();
????????clockThread2.start();
????????clockThread3.start();
????}
????public?void?run()
????{
????????boolean?flag=true;
????????while(flag)
????????{
????????????try
????????????{
????????????????if?(Thread.currentThread()?==?clockThread1){
????????????????????Thread.sleep(1000);
????????????????????f1?=?true;
????????????????}
????????????????if?(Thread.currentThread()?==?clockThread2){
????????????????????Thread.sleep(5000);
????????????????????f2?=?true;
????????????????}
????????????????if?(Thread.currentThread()?==?clockThread3){
????????????????????Thread.sleep(10000);
????????????????????f3?=?true;
????????????????}
????????????????super.repaint();
????????????}
????????????catch(InterruptedException?e)
????????????{
????????????????flag=false;
????????????}
????????}
????}
????public?void?paint?(Graphics?g)
????{
????????g.setColor(this.getBackground());
????????if?(f1){
????????????currentDate1=new?Date();
????????????lastdate1=formatter.format(currentDate1);
????????????g.fillRect(5?0?50?10);
????????????f1?=?false;
????????}
????????if?(f2){
????????????currentDate2=new?Date();
????????????lastdate2=formatter.format(currentDate2);
????????????g.fillRect(125?0?50?10);
????????????f2?=?false;
????????}
????????if?(f3){
????????????currentDate3=new?Date();
????????????lastdate3=formatter.format(currentDate3);
????????????g.fillRect(225?0?50?10);
????????????f3?=false;
????????}
????????g.setColor(getForeground());
????????g.drawString(lastdate1510);
????????g.drawString(lastdate212510);
????????g.drawString(lastdate322510);
????}
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????10545??2018-06-22?17:01??11.8(3)\20180622170129.png
?????文件????????2484??2018-06-22?17:01??11.8(3)\Clock.class
?????文件????????2487??2018-05-24?16:35??11.8(3)\Clock.java
?????文件???????19968??2018-06-22?17:02??11.8(3)\Thumbs.db
?????文件?????????141??2018-06-22?17:01??11.8(3)\java.policy.ap
?????目錄???????????0??2018-09-28?14:34??11.8(3)\
評論
共有 條評論