| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 837 人关注过本帖
标题:求各位高手帮忙把代码给弄出来!不胜感激
只看楼主 加入收藏
hjh363957702
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-6-17
收藏
 问题点数:0 回复次数:3 
求各位高手帮忙把代码给弄出来!不胜感激
编写3个线程分别各显示各自的运行时间,第1个线程每隔1秒运行1次,
第2个每隔5秒运行1次,第3个线程每隔10秒运行1次.
搜索更多相关主题的帖子: 感激 线程 代码 
2008-06-17 20:01
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:0 
以前给别人写的
感觉差不多
改改吧


import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.text.*;
public class Display extends JFrame{
    private JLabel tipLabel,timeText,timeText1,timeText2;
    private Container container;
    public Display (){
        container = getContentPane();
        container.setLayout(new GridLayout(4,1,5,5));
        tipLabel = new JLabel("各线程已运行时间:");
        timeText = new JLabel();
        timeText1 = new JLabel();
        timeText2 = new JLabel();
        container.add(tipLabel);
        container.add(timeText);
        container.add(timeText1);
        container.add(timeText2);
        
        try{
            Thread thread = new TimeDisplay(timeText,1);
            thread.start();
            Thread.sleep(1000);
            
            Thread thread1 = new TimeDisplay(timeText1,2);
            thread1.start();
            Thread.sleep(1000);
            
            Thread thread2 = new TimeDisplay(timeText2,3);
            thread2.start();
            Thread.sleep(1000);
        }
        catch(Exception e){
        }
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //pack();
        setSize(300,200);
        setVisible(true);
        
        //将窗口屏幕居中
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension frameSize = getSize(),screenSize = kit.getScreenSize();
        setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    }
    public static void main(String args[]){
        Display display = new Display();
    }
}

class TimeDisplay extends Thread {
    private JLabel timeField;
    private long start;
    private int number;
    
    public TimeDisplay(JLabel field,int num) {
        timeField = field;
        number = num;
        start = System.nanoTime();
    }

    public void run() {
        while(true){
            timeField.setText("线程"+number+"已经运行了"+String.valueOf((System.nanoTime()-start)/1000000000)+"秒");
            try{
                Thread.sleep(1000);
            }
            catch(Exception e){
            }
        }
    }
}
2008-06-17 22:44
hjh363957702
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-6-17
收藏
得分:0 
回复 2# freish 的帖子
非常感谢了!!!
2008-06-28 02:32
longrm
Rank: 1
等 级:新手上路
帖 子:129
专家分:0
注 册:2007-6-18
收藏
得分:0 
不错不错

java群: 55919698

My blog: http://hi.baidu.com/longrm
2008-06-30 10:28
快速回复:求各位高手帮忙把代码给弄出来!不胜感激
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013375 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved