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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:08:42+00:00 2026-05-22T20:08:42+00:00

In a swing application, I would like to re-utilize a spawned thread instead of

  • 0

In a swing application, I would like to re-utilize a spawned thread instead of creating a new one to serve requests. This is because the requests would be coming in short intervals of time and the cost of creating a new thread for every request could be high.

I am thinking of using the interrupt() and sleep() methods to do this as below and would like to know any potential performance problems with the code:

public class MyUtils {
    private static TabSwitcherThread tabSwitcherThread = null;

    public static void handleStateChange(){
        if(tabSwitcherThread == null || !tabSwitcherThread.isAlive()){
            tabSwitcherThread = new TabSwitcherThread();
            tabSwitcherThread.start();
        }
        else
            tabSwitcherThread.interrupt();      
    }

    private static class TabSwitcherThread extends Thread{
        @Override
        public void run() {
           try {
                 //Serve request code

                 //Processing complete, sleep till next request is received (will be interrupted)
                 Thread.sleep(60000);
           } catch (InterruptedException e) {
                //Interrupted execute request
                run();
           }

           //No request received till sleep completed so let the thread die         
        }
   }
}

Thanks

  • 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-22T20:08:43+00:00Added an answer on May 22, 2026 at 8:08 pm

    I think what you’re looking for is a ThreadPool. Java 5 and above comes with ThreadPoolExecutor. I would suggest you use what is provided with Java instead of writing your own, so you can save yourself a lot of time and hairs.

    Of course, if you absolutely has to do it the way you described (hey, sometimes business requirement make our life hard), then use wait() and notify() as Jon suggested. I would not use sleep() in this case because you have to specified timeout, and you never know when the next request will come in. Having a thread that keep waking up then go back to sleep seems a bit wasteful of CPU cycle for me.

    Here is a nice tutorial about the ThreadPoolExecutor.

    EDIT:

    Here is some code example:

    public class MyUtils {
        private static UIUpdater worker = null;
        private static ExecutorService exeSrv = Executors.newFixedThreadPool(1);
    
        public static void handleStateChange(){
            if(tabSwitcherThread == null || !tabSwitcherThread.isAlive()){
                worker = new UIUpdater();
            }     
    
            //this call does not block 
            exeSrv.submit(worker, new Object());     
        }
    
        private static class UIUpdater implements Runnable{
            @Override
            public void run() {
    
               //do server request and update ui.
            }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make my Java/Swing application compatible with the Services-menu available on
I would like to create an application wide keyboard shortcut for a Java Swing
i have a swing application that i would like to run on os x
I have a Swing application that I would like to convert from spaghetti to
I would like to launch a Scala Swing application from the command line, then
I would like to create extend a Java Swing application to have a look
I would like to be able to change the locale in my Swing application
I have made a Java Swing application. Now I would like to make it
I would like to enable HD video playback for my Java Swing application but
Is there any default functionality for arranging JInternalFrames in Java Swing? I would like

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.