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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:46:50+00:00 2026-05-10T23:46:50+00:00

My question pertains to multi-threading in Java. I’m translating an app I wrote in

  • 0

My question pertains to multi-threading in Java. I’m translating an app I wrote in Visual Basic 2008 into Java. There is a class in VB called BackgroundWorker, which allows the coder to perform a task on another thread, a lot like SwingWorker in Java. The only distinct difference is that, with the BackgroundWorker thread is run(), it fires an event called DoWork() on the mainline which contains the code to execute in the background. Furthermore, after the code has executed, a RunWorkerCompleted() event is fired back on the foreground thread to interpret results.

I have found the BackgroundWorker setup quite useful and it seems a little more flexible than SwingWorker and I was just wondering whether it was possible (and acceptable) to fire events in the same way in Java? And if so, how would I go about it? Since I’ve only done a quick scan over SwingWorker, it’s possible that it has a similar functionality that would work just as well, in which case I would be happy to know about that instead.

Cheers,

Hoopla

EDIT:

Kewl. Cheers guys, thanks for the prompt replies, and apologies for my rather time-lax reply. I’ll give your idea a go Oscar, sorry coobird, I didn’t quite follow – any further explanation would be welcome (perhaps an example).

Just to recap: I want to have a runnable class, that I instantiate an instance of in my code. The runnable class has two events, one of which is fired from the background thread and contains the code to run in the background (DoWork()), and the other event is fired on the foreground thread after the background thread has completed it’s task (RunWorkerCompleted()).

And if I understand your advice correctly, I can fire the DoWork() event from the runnable class’ run() method so that it will be executed on the background thread, and then I can use the SwingUtilities.invokeLater() method to fire the RunWorkerCompleted() event on the foreground thread, once the background thread has finished execution.

Yes?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T23:46:50+00:00Added an answer on May 10, 2026 at 11:46 pm

    The solely intention of the following code is to show how does it looks like when the SwingUtilities.invokeLater method is used.

    The effect is the task is executed in the AWT-Event thread ( the one responsible for component painting ) .

    The rest ( creating a new thread , creating a gui etc. ) is just scaffold code.

    import java.awt.*; import java.awt.event.*; import javax.swing.*;  public class InvokeLaterEffect {      static JTextArea text = new JTextArea();       // See the diff by commenting.     static void done() {         SwingUtilities.invokeLater( doneAction );          //doneAction.run();     }       public static void main( String [] args ) {         JFrame frame = new JFrame();         frame.add( text );         frame.pack();         frame.setVisible( true );          bacgroundTask.start();     }     // run a task in the background     static Thread bacgroundTask = new Thread(){         public void run(){             try {                  System.out.println( Thread.currentThread().getName() + ' Started background task ');                 Thread.sleep( 5000 );                 System.out.println( Thread.currentThread().getName() + ' Finished background task');                 done();             } catch ( InterruptedException ie ){}         }     };      // called whtn id done     static Runnable doneAction = new Runnable(){         public void run(){             System.out.println( Thread.currentThread().getName() + ' start setting text ');             text.setText('Hello');             System.out.println( Thread.currentThread().getName() + ' finish setting text ');         }     };   } 

    The output is like:

    Thread-2 Started background task Thread-2 Finished background task AWT-EventQueue-0 start setting text AWT-EventQueue-0 finish setting text 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently integrating external applications in my app ex SalesForce.com. My question pertains to
Question Is there a way to have a method that will always run anytime
This question pertains to Ruby on Rails and PHP. When looking for a VPS
This question pertains primarily to good design. Suppose I have a controller action like
Question as stated in the title.
Question is pretty self explanitory. I want to do a simple find and replace,
Question Alright, I'm confused by all the buzzwords and press release bingo going on.
Question in the title. And what happens when all 3 of $_GET[foo] , $_POST[foo]
Question Can I build a image database/library that has an e-commerce style checkout system
Question says it all, really. My application is a time tracker. It's currently written

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.