java - 我的线程不会在指定时间内 hibernate

标签 java multithreading wait sleep

我的程序应该让赛车“跑”一场比赛,然后打印出谁是第一名。

主要:

    /**
     * Load the racers and print them out.
     * @throws IOException 
     */
    private void run(String the_File) throws IOException {
        try {
            List<Racer> racers = RacerReader.read(the_File);
            Race(racers);
            RacerReport.write(racers);

        } catch (ApplicationException e) {
            LOG.error("An exception was thrown from RacerReader.");
            System.out.println(e.getMessage());
        } finally {
            printEndTimeAndDuration();
            LOG.info("End of Lab8.run().");
        }
    }

    /**
     * 
     * @param racers an array list of racers
     * 
     * This method will have the racers run a race and sort them into a list as they finish.
     * The list will be returned at the end.
     * 
     * @return a List of racers.
     */
    public List<Racer> Race(List<Racer> racers){

        List<Racer> finished_racers = new ArrayList<Racer>();

        for(Racer racer : racers) {
            racer.start();
            finished_racers.add(racer);
        }

        return finished_racers;

    }

Racer 类的 run():

    /**
     * The thread method.
     */
    public void run() {

        Instant start_running = Instant.now();  

                //random_number_generator is a field in Racer declared as:
                //Random random_number_generator = new Random(System.currentTimeMillis());
        Long randomNum = (long) (random_number_generator.nextInt((10 - 1) + 1) + 1);
        long delay = 90L + randomNum;

        try {
            sleep(delay * 10000);

            Instant finished_time = Instant.now();
            long duration = Duration.between(start_raceing, finished_time).toMillis();

            getFinished(this,duration);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * 
     * @param racer the racer.
     * @param result his time running the race.
     * @return the racer with a new randomized time.
     */
    private Racer getFinished(Racer racer, long result) {

        //update the default result with the new randomized result.
        racer.result = result; 

        //Return the racer with his new time
        return racer; 
    }

你可能会注意到 sleep(delay * 10000);延迟可能是 97 毫秒,你是对的,我看不到这一点,所以我添加了 10000 乘数来尝试减慢速度。

此外,当我在 Eclipse 中调试时,它会在断点上的 sleep() 行处停止;但是当我点击“继续”按钮进行更多调试时,它似乎没有激活 getFinished() 方法,这是一个问题,因为它代表我不断得到完全相同的结果。该结果是我从文本文件中提供给 main 中的赛车手的数据。

有什么想法吗?我尝试将 10,000 扩展到 1,000,000,000,但仍然需要几百毫秒。我也尝试使用 wait() 来代替,但它似乎也没有减慢速度。

最佳答案

90*10000 是 900 秒 - 15 分钟。你就知道了。

你确定你不是只是观察所有赛车手都僵住了,什么也不做吗? 15分钟是很长的时间。尝试乘以 30。现在每个赛车手大约需要 2.7 到 3 秒才能完成。

关于java - 我的线程不会在指定时间内 hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62462891/

相关文章:

java - intellij IDEA中如何打开或调用regexTester插件

python - Python 中的异步多线程抓取,线程有限

Java等待通知死锁问题

java - 并行执行线程超时

linux - Bash 等待多个同时的子进程并在出错时杀死所有子进程

java - Spring Boot Security 4 与 ZooKeeper - 安全集成问题

java - 按分数限制 SOLR 中的结果数量

java - 持久化新元素时违反唯一索引或主键

java - Mysql是否在一个连接中同步执行语句?

java - Java 中的轻量级线程?