0

Consider the following simple method which is part of an appication with Swing gui:

public void updateFromGoogle(int action)  {

    startTime =  System.currentTimeMillis();

    model.updateFromGoogle(action);

    System.out.println("Total run time "
    + (System.currentTimeMillis()-startTime));

}

Run time i=of the above method is consistently under 1 milli.
If I add to the method a SwingWorker that does nothing:

public void updateFromGoogle(int action)  {


    startTime =  System.currentTimeMillis();

    SwingWorker sw = new SwingWorker<Boolean, Void>(){

        @Override
        protected Boolean doInBackground() throws Exception {
            System.out.println("");
            System.out.println("Swing worker start after "
                    + (System.currentTimeMillis()-startTime));

            return null;
        }

        @Override
        public void done() {

            System.out.println("Swing worker finished after "
                    + (System.currentTimeMillis()-startTime));
        }

    };

    sw.execute();

    model.updateFromGoogle(action);

    System.out.println("Total run time "
    + (System.currentTimeMillis()-startTime));

}


Occasionally I can see that the SwingWorker starts after a very long time, for example (millis):

Swing worker start after 19734

Swing worker finished after 19767


Questions:
1. Does it suggest that some other long task is Occasionally running on the Event Dispatch Thread ?
2.How do I investigate which task is causing it ?

Edit: Following comment I installed jvisualvm that shows that SwingWorker uses a pool of 8 threads :

enter image description here

c0der
  • 18,467
  • 6
  • 33
  • 65
  • Possible duplicate of [*Busy loop in other thread delays EDT processing*](http://stackoverflow.com/q/35154352/230513); see update 6. – trashgod Oct 28 '16 at 09:22
  • I don't think it answers my question (it doesn't even answer the question posted there) – c0der Oct 28 '16 at 09:49
  • I didn't think so either; the bug report proved dispositive. – trashgod Oct 28 '16 at 09:57
  • @c0der probably your SwingWorker uses a single thread and this thread is busy by another background task? – Sergiy Medvynskyy Oct 28 '16 at 10:01
  • @SergiyMedvynskyy yes, I think this is expressed in question 1. I wonder how I verify it, and how to I investigate which other long process run on EDT. – c0der Oct 28 '16 at 10:09
  • Have a look at the stack traces, either using a Debugger or by a separate background Thread which prints the trace for [every thread](http://javadoc.imagej.net/Java8/java/lang/Thread.html#getAllStackTraces--). – Martin Nyolt Oct 28 '16 at 10:39
  • 1
    @c0der try to use `jvisualvm` utility, which is a part of JDK or another profiler. So you can see the number of threads. Swing worker threads have prefix `SwingWorker-`. If you have only 1 thread - it could be your problem – Sergiy Medvynskyy Oct 28 '16 at 10:41
  • @SergiyMedvynskyy Thanks. I will look up `jvisualvm` utility. (I thought that Swing should be single thread ?) – c0der Oct 28 '16 at 11:01
  • 1
    [to check code from my two answers here](http://stackoverflow.com/q/6171414/714968), I'm doubt in fack that Future is single threaded, there is delay in miliseconds, not in seconds, something went wrong with code inside your real code, maybe another code executions are without mentioned delay – mKorbel Oct 28 '16 at 14:00
  • @SergiyMedvynskyy following your comment I installed `jvisualvm` and I can see that `SwingWorker` uses a pool of 8 threads. I still do not know how to get more useful information out of the profiler. – c0der Nov 10 '16 at 06:49

1 Answers1

0

Following @SergiyMedvynskyy comment I installed jvisualvm.
By sampling cpu I found that another SwingWorker process is running for a long time, apparently blocking others :

enter image description here

c0der
  • 18,467
  • 6
  • 33
  • 65