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

  • Home
  • SEARCH
  • 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 428343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:39:59+00:00 2026-05-12T19:39:59+00:00

I have made a swings application but there is one problem in that which

  • 0

I have made a swings application but there is one problem in that which is as follow:

I have initiated a SwingWorker thread named “Thread-Main” from the Event Dispatch Thread and have passed a JLabel reference of the GUI to the “Thread-Main”.

Now I have started 10 threads from the “Thread-Main”.

Now I want that all the 10 threads should update the JLabel.

How can I do that?

Someone told me that I can do this by first making all the 10 threads subclasses of SwingWorker and then calling the publish(“”) method and passing the string in that “publish” method and then collecting all the published strings by the following method in “Thread-Main”

@Override
protected void process(List<String> labelStrings) {
    String count = labelStrings.get(labelStrings.size() - 1);
    label.setText(count); // label is a reference to a label in the GUI 
}
  1. Is the above approach is correct to do that?
  2. Should the 10 threads be subclasses of SwingWorker?
  3. Is there any other way to do that?
  • 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-12T19:40:00+00:00Added an answer on May 12, 2026 at 7:40 pm
    1. No – This is the incorrect approach if you want to control the number of threads yourself. Why? Because if you look at the SwingWorker code you’ll see that it uses a ThreadPoolExecutor internally containing a maximum of 10 threads. If you kick off a number of SwingWorker‘s concurrently they will all run using this executor. However, you have no direct control over whether the background threads are executed in parallel.
    2. See point 1.
    3. My recommended solution would be:

      • Create a single SwingWorker.
      • Within the doInBackground() method kick off 10 threads either directly or by using an ExecutorService.
      • Use a CountDownLatch or CompletionService to synchronize between your master thread (i.e. SwingWorker background thread) and worker threads.

    Example

    Define the number of worker threads to invoke and declare the JLabel to be updated.

    final int nThreads = 10;
    JLabel myLbl = new JLabel();
    

    Define the unit of work we wish to execute as a Callable<String>. The String result will be used to update the JLabel.

    private static class MyCallable implements Callable<String> {
      public String call() { ... }
    }
    

    Now kick off a SwingWorker, which will in turn kick off multiple parallel workers to do any processing. The worker will not return a result via the call to done() (hence the Void type) but will marshall intermediate String results back onto the Swing thread by calling process(String... chunks).

    new SwingWorker<Void, String>() {
      // See method definitions below.
    }.execute();
    

    Define doInBackground() to kick off the worker threads and block for each result using a CompletionService.

    public Void doInBackground() throws Exception {
      // Define executor service containing exactly nThreads threads.
      ExecutorService execService = Executors.newFixedThreadPool(nThreads);
    
    
      // Define completion service that will contain the processing results.
      CompletionService compService = new ExecutorCompletionService(execService);    
    
      // Submit work to thread pool using the CompletionService.  Future<String>
      // instances will be added to the completion service's internal queue until complete.
      for (int i=0; i<nThreads; ++i) {
        compService.submit(new MyCallable());
      }
    
      // Take results from each worker as they appear and publish back to Swing thread.
      String result;
      while ((result = compService.take().get()) != null) {
        publish(result);
      }
    }
    

    Now we implement process(String... chunks) to simply update the JLabel when called.

    public void process(String... chunks) {
      if (chunks.length > 0) {
        // Update label with last value in chunks in case multiple results arrive together.
        myLbl.setText(chunks[chunks.length - 1]);
      }
    }
    

    Finally we override done() to marshall any exceptions back onto the Swing thread.

    public void done() {
      try {
        get(); // Will return null (as per Void type) but will also propagate exceptions.
      } catch(Exception ex) {
        JOptionPane.show ... // Show error in dialog.
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made some changes to a Classic ASP application which breaks foreign letters
I have made a SVG image, or more like mini application, for viewing graphs
I have made some code which exports some details of a journal article to
I have made a custom UserControl i Vb.net (windows application). How can I add
I have made a new windows service which works fine using barebone code (just
So I have made a webservice that interfaces with a set of data contained
I have a JavaScript class that I have made and put it into its
I have made a TForm derivative that acts like the drop down part of
I have made the following linked list which is not printing the list .The
I have made a little app for signing up for an event. User input

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.