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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:43:12+00:00 2026-06-11T03:43:12+00:00

In another question I posted recently I was suggested to use a java.util.Timer in

  • 0

In another question I posted recently I was suggested to use a java.util.Timer in the SwingWorker instead of a while loop. The tasks in the SwingWorker are supposed to run every 10 seconds.

From my understanding, java.util.Timer creates a background thread (run()) to perform tasks.

If I use the java.util.Timer in the SwingWorker thread, isn’t it inefficient to create another thread (Timer) in a thread (SwingWorker)?

Please take a look at my two examples below and let me know which one is the correct one, or if none of the two are correct what else I should use (and please provide me with a short sample code if possible)? Thank you

Example 1 – using java.util.Timer

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.SwingWorker;

public class Example1 extends SwingWorker<Void, String> {

    @Override
    protected Void doInBackground() {

        int delay = 10000;//Delay for 10 seconds

        Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                String database = "";

                //PARSE THE SQL DATABASE

                publish(database);//UPDATE THE GUI

            }
        }, delay);

        return null;
    }

    @Override
    protected void process(List<String> s) {
        //UPDATE THE GUI
    }
}

Example 2 – using While loop

import java.util.List;
import javax.swing.SwingWorker;

public class Example2 extends SwingWorker<Void, String> {

    @Override
    protected Void doInBackground() {

        while (!this.isCancelled()) {

            String database = "";

            //PARSE THE SQL DATABASE

            publish(database);//UPDATE THE GUI

            try {
                synchronized (this) {
                    this.wait(10000);//Wait 10 seconds
                }
            } catch (Exception ex) {
            }

        }

        return null;
    }

    @Override
    protected void process(List<String> s) {
        //UPDATE THE GUI
    }
}
  • 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-06-11T03:43:14+00:00Added an answer on June 11, 2026 at 3:43 am

    If the only thing that SwingWorker is doing is to launch a timer then it’s useless indeed. Just spawn the timer from the UI thread and use SwingUtilities.InvokeLater to update the GUI from the timer.

    An even better alternative is ScheduledThreadPoolExecutor.

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

Sidebar

Related Questions

This question is related to another one I've posted recently: Check printing with Java/JSP
From another question I recently posted, it seems that Classic ASP might not be
I have recently posted another question which straight away users pointed me in the
This question is related to another one that I posted recently: Understanding HttpServletRequest and
This question is related to another question I just posted . I'm prepping for
In another question, a user posted the following reflog: 8c48bab HEAD@{16}: checkout: moving from
An answer posted for one of my previous questions brings up another question; I
Following on from a question I posted yesterday about GUIs, I have another problem
Piggy backing off another question I posted , I have a complex find() that
This is a spin off from another question I posted a few days ago

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.