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 1043673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:42:20+00:00 2026-05-16T15:42:20+00:00

I am not clear about thread confinement. In swing all the gui components must

  • 0

I am not clear about thread confinement.
In swing all the gui components must be updated through the EDT. SwingWorker is provided in Java6 for lengthy operations, and in the done method the gui components can be updated. My understanding was that the gui components in the done() method are updated in the EDT. Therefore there should be no synchronization problems. But here link text

it says:

Because the ImageRetriever class will
download an image and place it on a
large label, providing the label and
image URL in the constructor is
convenient. ImageRetriever needs the
URL to retrieve an image. Provide the
label so that the ImageRetriever
instance can set the label’s icon
itself. If you use inner classes, you
might not even provide this
information in the constructor,
because the worker thread will be able
to access the information directly.
However, providing the information in
the constructor helps your application
to be more thread-safe because that
information will not be shared among
ImageRetriever instances

I am confused on this. If the SwingWorker methods update the gui components (in the example of the link the JLabel) in the EDT, why is it more thread-safe to not share them among ImageRetriever(=SwingWorker) instances? If we have multiple SwingWorkers and in the done() method they update the same component, we must use synchronization primitives for the update? Am I missunderstanding something? Doesn’t thread-confinement mean that only 1 thread will do all the actions? Aren’t swingworkers thread-confined?

Thanks

  • 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-16T15:42:21+00:00Added an answer on May 16, 2026 at 3:42 pm

    If the label is declared in the parent class and for some reason a new value is assigned to that variable at some point, then all the swingworkers will see the update. Because this might happen while the EDT is updating things, it can lead to weird behaviors.

    For instance:

    SW in EDT - label.setText(...);
    Thread1 - label = new JLabel();
    SW in EDT - label.setIcon(...);
    

    If the label variable is shared you will get an inconsistent state (label without text or icon).

    Update
    Storing the label as a variable passed through the constructors is a way to avoid this issue. If you want to see changes but not in the middle of a method execution, on way is to use a method local variable. You assign it at the beginning of the method to make sure it’s not going to be changed outside.

    If you use inner classes and the attribute of the parent class this will look like this:

    public void done() {
      JLabel l = label;
      l.setText(...);
      l.setIcon(...);
    } 
    

    If the variable is defined as an attribute of the SW, you will have to create some way of getting the value stored in the main class (eg. a getter)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.