开发者

Wait function in Java

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 wa开发者_运维问答it 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.

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


You're looking for Thread.sleep(int milliseconds)


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

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()


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)

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

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.


You can try Thread.sleep(1000);

It will make the current thread sleep for 1000 milliseconds.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜