Java wait function

So, I write Java code to represent heap sorting and present an operation in which I need a wait function that will wait between different operations, but I'm not sure if there is a function in Java that does this or does it. I need to write the function myself and how do i do that.

Presenting the heap of sports is homework, but recording the wait function is wrong, I appreciate your help.

+3
source share
6 answers

You are looking for Thread.sleep(int milliseconds)

+14
source

, parallelism , . , ? "", , , fork/join (Java 7)

, this (IBM Java guru):

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;
    }
}

, parallelism , Thread.Sleep(int miliseconds).

+3

Thread.sleep() - , .

, , , , Java. , , reset Thread.currentThread(). Interrupt()

+2

Thread.sleep(1000);

1000 .

+2

(, , ) , :

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

, , , , . . .

0

:

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

1000 1 . Java.

0

Source: https://habr.com/ru/post/1738512/


All Articles