Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5973559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:49:01+00:00 2026-05-22T20:49:01+00:00

Javadoc of the done() method of SwingWorker: Executed on the Event Dispatch Thread after

  • 0

Javadoc of the done() method of SwingWorker:

Executed on the Event Dispatch Thread after the doInBackground method
is finished.

I’ve clues that it is not true in the case of canceled worker.
Done is called in each case (normal termination or cancellation) but when cancelled it is not enqueued to the EDT, as it happens for normal termination.

Is there some more precise analisis on when done is called in the case that a SwingWorker is cancelled?

Clarification:
this question is NOT on how to cancel a SwingWorker. Here it is assumed that the SwingWorker is cancelled in the right way.
And it is NOT about thread still working when they are supposed to be finished.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:49:02+00:00Added an answer on May 22, 2026 at 8:49 pm

    When a thread is cancelled via

    myWorkerThread.cancel(true/false);
    

    the done method is (quite surprisingly) called by the cancel method itself.

    What you may expect to happen, but actually DOESN’T:
    – you call cancel (either with mayInterrupt or not)
    – cancel set up the thread cancellation
    – the doInBackground exits
    – the done is called*
    (* the done is enqueued to the EDT, that means, if EDT is busy it happens AFTER the EDT has finished what it is doing)

    What actually DOES happen:
    – you call cancel (either with mayInterrupt or not)
    – cancel set up the thread cancellation
    – the done is called as part of cancel code*
    – the doInBackground will exit when it will have finished its loop
    (*the done isn’t enqueued to the EDT, but called into the cancel call and so it has a very immediate effect on EDT, that often is the GUI)

    I provide a simple example that proves this.

    Copy, paste and run.
    1. I generate a runtime exception inside done. The stack thread shows that done is called by cancel.
    2. About after 4 seconds after cancelation, you’ll recive a greeting from the doInBackground, that fhurterly proves that done is called before the thread exiting.

    import java.awt.EventQueue;
    import javax.swing.SwingWorker;
    
    public class SwingWorker05 {
    public static void main(String [] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                W w = new W();
                w.execute();
                Thread.sleep(1000);
                try{w.cancel(false);}catch (RuntimeException rte) {
                    rte.printStackTrace();
                }
                Thread.sleep(6000);
                } catch (InterruptedException ignored_in_testing) {}
            }
    
        });
    }
    
    public static class W extends SwingWorker <Void, Void> {
    
        @Override
        protected Void doInBackground() throws Exception {
            while (!isCancelled()) {
                Thread.sleep(5000);
            }
            System.out.println("I'm still alive");
            return null;
        }
    
        @Override
        protected void done() {throw new RuntimeException("I want to produce a stack trace!");}
    
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In javadoc there is said that yield method Causes the currently executing thread object
I see from the javadoc that the @SuppressWarnings annotation applies to TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE targets. Why
The javadoc for SimpleDateFormat states that SimpleDateFormat is not synchronized. Date formats are not
I need to put some extra information into to the standard javadoc hover, that
The javadoc for org.joda.time.format.ISODateTimeFormat.dateTime() is: Returns a formatter that combines a full date and
The Javadoc for EventListenerLists says that they offer a degree of MT safety (when
Javadoc for Class.getFields() say: The elements in the array returned are not sorted and
Reading the javadoc for EhCacheManagerFactoryBean and EhCacheFactoryBean I figured that: <bean id=cacheManager class=org.springframework.cache.ehcache.EhCacheManagerFactoryBean p:configLocation=classpath:ehcache.xml/>
JavaDoc defines set as : A collection that contains no duplicate elements. More formally,
NumberFormat JavaDoc says: Number formats are generally not synchronized. It is recommended to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.