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 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

Ask A Question

Stats

  • Questions 201k
  • Answers 201k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The "inference engine" (what I believe you are calling the… May 12, 2026 at 8:13 pm
  • Editorial Team
    Editorial Team added an answer The clean workaround is to use ExecutorService.submit() instead of execute().… May 12, 2026 at 8:13 pm
  • Editorial Team
    Editorial Team added an answer I'm not sure how you would get at the JSON… May 12, 2026 at 8:13 pm

Related Questions

My question pertains to multi-threading in Java. I'm translating an app I wrote in
I have an app that shows store sales. It is a multi-dimensional array, so
Does Safari have anything equivalent to the HTML 5.0 spec for window.navigator.online? http://www.whatwg.org/specs/web-apps/current-work/#browser-state If
I need to replace character (say) x with character (say) P in a string,
I have an anchor that doesn't go anywhere but has an onclick (e.g. <a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.