半块板砖 2018-11-24 13:25 采纳率: 0%
浏览 377

多线程唤醒时间问题,想用millis来测一个运行时间,用作唤醒,没成功。问题在哪?

public class Double implements Runnable {

    private int time; 
   public Double (int time) {
       this.time = time;
   }

    public Double() {

    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    @Override
    public void run() {

        List list = new ArrayList<>();
        Long millis1= System.currentTimeMillis();   
        for (int i = 0; i < 100; i++) {
            int rand = (int)(Math.random()*100);

            System.out.println(Thread.currentThread().getName()+":"+rand);

            list.add(rand);
            try {
                Thread.sleep((int)(Math.random()*200));
            } catch (InterruptedException e) {
                System.out.println("醒来");
            }

        }
        Long millis2= System.currentTimeMillis();   

        time  = (int)(millis2-millis1);

        System.out.println("经过时间:"+time);


        try {
            Thread.sleep(10*1000);
        } catch (Exception e) {
            System.out.println("睡10秒");
        }
        System.out.println(list);
    }


    }

////////////////////////////////////////////////////这是一条分割线
public class DoubleTest {
    public static void main(String[] args) {

        Runnable runner = new Double();//必须有构造器,系统加
        Thread thread1 = new Thread(runner);
        thread1.setName("随机整数");
        thread1.start();


        if(((Double)runner).getTime()!=0) {

            try {
                thread1.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            thread1.interrupt();

        }

    }
}
  • 写回答

1条回答 默认 最新

  • dabocaiqq 2018-11-24 14:39
    关注
    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?