2 years ago

#16328

test-img

Tomek Nałęcz

JAVA code doesn't run because of many threads

I have a small problem.
I wanted to draw something in JPanel using paintComponent() but I wanted the objects to be drawn with a delay. I realized that Thread.sleep() doesn't work to make that delay so I made a method:

public void Do(int o) {
    
    final ScheduledExecutorService schedulerIn = Executors.newScheduledThreadPool(0);
    schedulerIn.schedule(new Runnable() {
        @Override
        public void run() { 
            order = o;
            repaint();
            schedulerIn.shutdownNow();
        ;}
    }, 1000, MILLISECONDS);
    
}

"order" is defying what will be drawn.
I want to use this method many times in a code;
Unfortunately the program doesn't run(the frame doesn't show up) and the usage of processor is increasing. I have a feeling that this method is a problem.
Do you have any idea how to optimize it or do something else?

java

multithreading

paintcomponent

scheduledexecutorservice

0 Answers

Your Answer

Accepted video resources