简体   繁体   中英

How to add breakpoints to Runnable classes

My while(true) is only running once, so I'm trying to add breakpoints to see what's going on, but they never seem to be reached within my run() . I'm using IntelliJ. In the debugger there's a "Threads" tab. Do I need to do something in that tab like select the right thread in order for my breakpoint to be reached? I also see thread names and am wondering how I can find the right thread in this list.

public class MyClass extends ServerWorkflowProcess<OtherClass> {

    private ExecutorService executorService = Executors.newSingleThreadExecutor();

    ...

    @Override
    public void bootup() {
        logger.info("Booting up: " + this);

        BackgroundProcess backgroundImpositioner = new BackgroundProcess(this.getCollection());
        executorService.submit(backgroundImpositioner);
    }

    @Override
    public void shutdown() {
        executorService.shutdown();
    }
}

Background process

public class BackgroundProcess implements Runnable {

    protected volatile Logger logger = Logger.getLogger(BackgroundImpositioner.class.getName());

    Collection<ImpositionWorkstation> impositionWorkstations;

    public BackgroundImpositioner(Collection<ImpositionWorkstation> impositionWorkstation) {
        this.impositionWorkstations = impositionWorkstation;
    }

    public void run() {
        while(true) {
            logger.info("looping");
            for (ImpositionWorkstation workstation : impositionWorkstations) {
                if (workstation.canAcceptWork()) {

                    //go do work in another thread so we're not blocking this
                    workstation.getWorkFromQueue();
                    try {
                        workstation.doWork();
                    } catch (ImpositionException e) {
                        logger.severe(e.getMessage());
                    }
                }
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                logger.severe("Background impositioner was interrupted");
            }
        }
    }
}

Side note: the console shows "looping", so I know it gets executed once. The breakpoint never gets hit and it doesn't execute more than once.

It happened to me once that i couldn't make Intellij Idea stop in breakpoints. Basically the problem is that once a thread is stopped in a breakpoint the others won't stop.

There is a setting in the breakpoints properties dialog that prevents this.

Right click on a breakpoint and select 'View Breakpoints'.

On the dialog select a breakpoint.

You will notice on the right of suspend checkbox 2 radio buttons: All and Thread . Select All . Aditionally you can make that the default ( Make Default button on the right). The default value will be used for any new breakpoints you add. The old ones need to be changed manually.

EDIT Additional info on the Intellij help site: Breakpoint options

断点对话框

Don't let exceptions slip through silently. Use the Future returned by the submit method.

Future<?> f=executorService.submit(backgroundImpositioner);
try {
  f.get();
} catch(Exception ex) {
  ex.printStackTrace();
}

Then you know more.

The code above is just for finding your actual problem. For production environments you wouldn't wait for completion but rather log the exception when it occurred, eg:

executorService.execute(new FutureTask<Object>(backgroundImpositioner, null)
{
  @Override
  protected void done() {
    if(!isCancelled()) try {
      get();
    } catch(InterruptedException ex) {
      throw new AssertionError("on completed task", ex);
    } catch(ExecutionException ex) {
      logger.log(Level.SEVERE, "in background task", ex.getCause());
    }
  }
});

出于某种原因,我无法在while(true)行中设置断点,但可以在run()其他位置删除断点。

If there's no exception thrown inside the run method, i can only assume that one of the calls never returns.

Can you put output statements after every single call to see how far you get? I guess either workstation.canAcceptWork() or workstation.doWork() is the culprit.

I've met a similar problem that IntelliJ never hits my breakpoint in the run() method of a Runnable class. I found that the only position for the breakpoint to be hit is at the line of public void run() { .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM