• 大小: 29KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-18
  • 语言: 其他
  • 标签: java  

资源简介

编写三个线程:各线程分别显示各自的运行时间,第一个线程每隔1秒钟运行一次,第二个每隔5秒钟运行一次,第三个线程每隔10秒钟运行一次。

资源截图

代码片段和文件信息

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);
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件       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.applet
     目录           0  2018-09-28 14:34  11.8(3)\

评论

共有 条评论