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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:18:49+00:00 2026-05-24T14:18:49+00:00

Update I’ve updated this question to include source code of a Java implementation, using

  • 0

Update

I’ve updated this question to include source code of a Java implementation, using the suggested SwingWorker class, to accomplish the same result as the Objective-C example. Hopefully this will be of aid to future adventurers.

Document myDoc = ...;
Model myModel = ...;

SwingWorker analyzeDocument = new SwingWorker<Dictionary, Void>() {
    @Override
    public Dictionary doInBackground() {
        return myDoc.analyze();
    }

    @Override
    public void done() {
        try {
            stats = get();
            myModel.setDict(stats);
            myModel.setNeedsDisplay(true);
        } catch(InterruptedException ex) {
            // log
        } catch(ExecutionException ex) {
            // log
        }
    }
};

analyzeDocument.execute();

Original Post

I’m inexperienced when it comes to concurrent programming and I was hoping someone may be able to explain to me how I can achieve this.

In Objective-C (with GCD), you’re able to do something like this:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{
     NSDictionary *stats = [myDoc analyze];
     dispatch_async(dispatch_get_main_queue(), ^{
       [myModel setDict:stats];
       [myStatsView setNeedsDisplay:YES];
       [stats release];
     });
 });

This code will execute [myDoc analyze] in the background thread, following a callback function to update the UI which will executed in the main thread. In other words, the background thread sends an interrupt to the main thread, adding the anonymous function to the main thread’s queue to be invoked. Obviously I won’t be able to use anonymous functions in Java, but that’s beside the point.

I am interested in doing something to this effect in Java. I have a Runnable object which does a bunch of stuff in the file system. When it finishes, I want to update the UI accordingly.

In an effort to not hang the main thread while doing this (ie: backgroundThread.join();), I’ve set the background thread to execute a callback function to update the UI. But this isn’t a good idea, I don’t want a non-GUI thread updating the GUI.

Some other ideas I’ve thought of is polling, but that seems like throwing cycles out the window. By the same judgement, using Futures doesn’t seem to be the answer either. This all seems to defeat the purpose of an asynchronous action.

The only other thing I can think of is using SwingUtilities.invokeLater from the background thread and using it to update the GUI. But I’m curious in which thread this would be executed.

Maybe my perception is just warped, but this seems like it would be a pretty big part to leave out. Am I just trying to go about this the wrong way?

  • 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-24T14:18:51+00:00Added an answer on May 24, 2026 at 2:18 pm

    Have you looked into creating and using a SwingWorker object? This allows you to easily run code in a thread background to the event thread, and in fact is a Future object as well and so it can return a result, and you can use this result in its done() method to update the GUI on the event thread. You can even do calls on the Swing event thread in the middle of its run via the publish/process method pair.

    For more on this, please check out: Concurrency in Swing

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

Sidebar

Related Questions

Update: Answers to this question helped me code the open sourced project AlicanC's Modern
Update: according to the stack trace, posted at the end of this question, I
UPDATE: I've edited the code below to show what I was using when I
I am using Paperclip to handle profile photo uploads in my app. They upload
UPDATE 14 June 2011 A quick update... Most respondents have focused on the dodgy
UPDATE userpostratings SET rating = $rating WHERE postID = 8 If a there isnt
UPDATE 2: OK, looks like it runs the first time after a minute. How
UPDATE Thanks to feedback below I was able to home in on ADPlus.vbs, which
UPDATE: Please see the updates below, as I'm adding more information to the bottom
UPDATE: navigating to the process via menu: DEBUG / Attach Process / iexplore.exe shows

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.