The most efficient method for a thread waiting for a certain time in Java

I know this question here , but I have a slightly different question. If I want to manually pass through various Thread methods (and not through utility classes or Quartz) the Thread start at a certain point in time, then what would be the most effective (in terms of overhead) for encoding it.

I thought:

    boolean wasInterrupted = false;

    while (System.currentTimeMillis() < executionTimeInMillis) {
        try {
            Thread.sleep(X);
        } catch (InterruptedException ie) {
            wasInterrupted = true;
        }
    }

    if (!wasInterrupted) {
        doMyThing();
    }

Is there a better way? Is it primitive and naive?

+3
source share
2 answers

You have 3 main features:

- , , . , . , , http://www.javamex.com/tutorials/threads/yield.shtml.

+3

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


All Articles