Java中的等待函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2517022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:43:05  来源:igfitidea点击:

Wait function in Java

java

提问by user220755

So I am writing a Java code to represent a heap sort and to represent the operation I need to have a waiting function which will wait between different operation but I am not sure if there is a function in Java that does that or do I need to write the function by myself and how would i do that.

所以我正在编写一个 Java 代码来表示堆排序并表示操作我需要一个等待函数,它将在不同的操作之间等待,但我不确定 Java 中是否有一个函数可以做到这一点,或者我是否需要自己写这个函数,我该怎么做。

Representing heap sport is a homework but writing the waiting function isn't so I appreciate your help

表示堆运动是一项家庭作业,但编写等待功能不是,所以我感谢你的帮助

采纳答案by SLaks

回答by Christopher Oezbek

Thread.sleep()is what you are looking for.

Thread.sleep()就是你要找的。

The exception it throws will likely puzzle you if you are a Java newbie. Most likely you will want to propagate it up or discard it and reset the interrupt flag using Thread.currentThread().interrupt()

如果您是 Java 新手,它抛出的异常可能会让您感到困惑。您很可能希望向上传播或丢弃它并使用 Thread.currentThread().interrupt() 重置中断标志

回答by tonio

You can try Thread.sleep(1000);

你可以试试 Thread.sleep(1000);

It will make the current thread sleep for 1000 milliseconds.

它将使当前线程休眠 1000 毫秒。

回答by XpiritO

If I understood you well, you want to create parallelism on different task execution and then wait for the completion of all of them to continue. Is this what you want? If your answer is "yes", then maybe you could use the "fork/join" framework (Java 7)

如果我理解你的话,你想在不同的任务执行上创建并行性,然后等待所有任务的完成继续。这是你想要的吗?如果您的回答是“是”,那么也许您可以使用“fork/join”框架(Java 7)

Here is a code snippet, taken from thisBrian Goetz (IBM Java guru) article:

下面是一段代码片段,摘自这篇Brian Goetz(IBM Java 大师)文章:

public class MaxWithFJ extends RecursiveAction {
    private final int threshold;
    private final SelectMaxProblem problem;
    public int result;

    public MaxWithFJ(SelectMaxProblem problem, int threshold) {
        this.problem = problem;
        this.threshold = threshold;
    }

    protected void compute() {
        if (problem.size < threshold)
            result = problem.solveSequentially();
        else {
            int midpoint = problem.size / 2;
            MaxWithFJ left = new MaxWithFJ(problem.subproblem(0, midpoint), threshold);
            MaxWithFJ right = new MaxWithFJ(problem.subproblem(midpoint + 
              1, problem.size), threshold);
            coInvoke(left, right);
            result = Math.max(left.result, right.result);
        }
    }

    public static void main(String[] args) {
        SelectMaxProblem problem = ...
        int threshold = ...
        int nThreads = ...
        MaxWithFJ mfj = new MaxWithFJ(problem, threshold);
        ForkJoinExecutor fjPool = new ForkJoinPool(nThreads);

        fjPool.invoke(mfj);
        int result = mfj.result;
    }
}

Otherwise, if don't want any parallelism and just want to wait some time, use Thread.Sleep(int miliseconds)function.

否则,如果不想要任何并行性而只想等待一段时间,请使用Thread.Sleep(int miliseconds)函数。

回答by MrOverkill

There's a simpler ( But possibly not better ) way to sleep that I have been using:

我一直在使用一种更简单(但可能不是更好)的睡眠方式:

public static void sleep(int amt) // In milliseconds
{
    long a = System.currentTimeMillis();
    long b = System.currentTimeMillis();
    while ((b - a) <= amt)
    {
        b = System.currentTimeMillis();
    }
}

This will essentially cause everything your program is doing to cease until the time runs out. It also can cause a bit of lag. You have been warned.

这基本上会导致您的程序所做的一切停止,直到时间用完。它也可能导致一些滞后。你被警告了。

回答by Caleb Woodman

Here's something a little more in depth:

这里有一些更深入的内容:

try {
    Thread.sleep(1000); 
} catch (InterruptedException e) {
    e.printStackTrace();
}

Here, we've inserted 1000 as the milliseconds value, or 1 second wait before running the rest of your code. This works with any program in Java.

在这里,我们插入了 1000 作为毫秒值,或者在运行其余代码之前等待 1 秒。这适用于任何 Java 程序。