1

I've done some search here and couldn't find an answer, so I think it's better to ask. I'm running a little bit expensive algorithm in a simple Java swing application. Let me describe the structure:

In my JPanel run() method:

public void run() {
    while(true) {
        Algorithm alg = new Algorithm(signal);
        new Thread(alg).start();

        //Wait for algorithm to finish 
        signal.await(alg);

        updateInterface();

        Thread.sleep(60L);
    }
}

Algorithm loops through the pixels of a .JPG file, then loops through another large Integer array (length ~ 12000) and returns. There are very no extra expensive calculationslot. I call Thread.sleep(60L) in the Algorithm run() method also.

The udpateInterface() method is very fast, just draw some java.awt.Polygon objects.

Even though I'm calling Thread.sleep(60L), the CPU usage is about 160% on my Mac Book (2.4 GHz Intel Core 2 Duo, Mem 4GB 1067).

Is there a way I can run this without melting my computer? I'm using CountDownLatch as a wait notify mechanism.

Thanks!

9
  • Thread.sleep is affecting the parent thread (the one with the while shown), not the new thread (new Thread)... the code might actually be spawning multiple threads at once, depending upon signal and all. Commented Mar 15, 2012 at 4:58
  • Yes, but the Algorithm thread also has a sleep call. Commented Mar 15, 2012 at 5:02
  • 1
    I don't understand what the purpose of the sleep call is. If you have work to be done, why are you sleeping"? What do you mean "melting my computer?! Work can't melt your computer. The purpose of the computer is to do work. If you have useful work to be done, make the CPUs do it. That's what they're for. Commented Mar 15, 2012 at 5:07
  • 1
    @pst: In other words, their capability is wasted. The top of the envelope is there so they can do lots of work very fast. If you're inefficient or wasting CPU, that's one thing. But if you have useful work to do, you want the CPU to do it as quickly as possible. Commented Mar 15, 2012 at 5:17
  • 2
    If you are concerned about CPU load you should figure out what is causing the high load. It looks like it's your algorithm. So what do you want to do about that? Not run it? Commented Mar 15, 2012 at 5:26

4 Answers 4

4

I would use the following pattern to schedule a repeating task.

private ScheduledExecutorService executorService = null;

public void start() {
    if (executorService != null && !executorService.isShutdown()) return;

    executorService = Executors.newSingleThreadScheduledExecutor();
        executorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                Algorithm alg = new Algorithm(signal);
                alg.run();
                updateInterface();
            }
        }, 0, 60, TimeUnit.MILLISECONDS);
}

public void stop() {
    if (executorService != null)
        executorService.shutdown();
}
Sign up to request clarification or add additional context in comments.

Comments

2

160% CPU usage is relative to a single core of your machine -- that is, the maximum possible on your machine is 200%, because it has two cores. You're not melting your processor.

9 Comments

Thanks for your comment. Sometimes it get's very close to 200%, i dont know if this will damage the computer...
No. This is perfectly normal -- in fact, 200% usage is a good sign that your application is parallelizing the work effectively.
@LouisWasserman Or wasting energy effectively ;-) Keeping the CPUs busy isn't quite the same as keeping them productive... in any case, I concur that the CPUs have something to do, which isn't necessarily bad. However, the question remains: How to make them "work less" (take longer) while solving this?
Unless there is something very wrong with the computer, you can't damage it by giving it computing work to do.
@pst: A user saying "I really wish my CPU was more idle" is about as common as lawyer saying "I'm so glad my client talked to the police". It just doesn't happen. If there's work to be done, you want your computer to do it.
|
2

There's no point starting another thread if all you're going to do afterward is make the current thread wait for the other one to finish. You might as well just run the algorithm in the thread you already have; either way, you won't proceeed to updateInterface() until the algorithm is done.

As others have pointed out, after the algorithm finishes and you update the UI, you're only waiting 60 milliseconds before starting the algorithm again. It sounds like your program is spending most of its time running the algorithm. That's fine if you need it to update the screen that quickly, but you might consider using a longer delay otherwise.

Also, you're starting a new thread each time through the loop. Does that thread run the algorithm once and then terminate, or does it run the algorithm in a loop? If you have a loop starting threads that are each long-running CPU-intensive loops, you might be accidentally running many copies of the algorithm at once. If you expect the algorithm thread to terminate after it signals you, you should join() it to make sure.

1 Comment

The Thread runs the algorithm and finishes. I already tried to run in the same Thread, and it increases the cpu usage. The synchronization is OK, what i need is to redesign this. Thanks!
1

How much wait do you want? If you want to wait for 60 seconds, you should use 60000L, as the time is specified in milliseconds.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.