4

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.

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

user220755
  • 4,358
  • 16
  • 51
  • 68
  • where do you need such function in heap sort? – Andrey Mar 25 '10 at 15:50
  • Do you want a time delay, or are you looking for a way of signaling when an operation is complete? – Michael Myers Mar 25 '10 at 15:51
  • I am representing it (drawing the operation) so in order to do that I want to have a waiting time between different operations for the user to be able to understand what's going on – user220755 Mar 25 '10 at 15:51
  • if this sleep command is used then it goes to sleep indefinitely... and Thread.currentThread().interrupt() isnt helping either.... –  May 17 '13 at 13:42

6 Answers6

16

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

luiges90
  • 4,493
  • 2
  • 28
  • 43
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    the code gives me "unreported exception java.lang.InterruptedException; must be caught or declared to be thrown" – user220755 Mar 25 '10 at 16:00
  • 1
    As the error indicates, you need to either catch the InterruptedException or rethrow it. – matt b Mar 25 '10 at 16:19
  • @OP: Put it in a try-catch block. `try {Thread.sleep(500);} catch(InterruptedException intrx) {/* handle the exception */}` – missingfaktor Mar 25 '10 at 16:22
  • My apologies SLaks never spotted that – Paul Whelan Mar 25 '10 at 16:38
  • @SLaks I used your above command it and works. But I don't actually understand it. If I'm not purposefully writing multi-threaded code, what is `Thread.sleep` doing? – john_science Dec 20 '12 at 17:41
  • @theJollySin: It sleeps for some time. http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep(long) – SLaks Dec 20 '12 at 17:42
  • @SLaks Thanks. I didn't realize that all Java programs had a primary `Thread` running, at all times. – john_science Dec 20 '12 at 17:48
  • @theJollySin: All code runs on a thread. Multi-threaded programming means having multiple threads that interact with eachother. – SLaks Dec 20 '12 at 17:51
3

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

Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
3

You can try Thread.sleep(1000);

It will make the current thread sleep for 1000 milliseconds.

tonio
  • 10,355
  • 2
  • 46
  • 60
3

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.

XpiritO
  • 2,809
  • 5
  • 28
  • 34
2

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.

MrOverkill
  • 53
  • 8
1

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.

Sjon
  • 4,989
  • 6
  • 28
  • 46