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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:43:54+00:00 2026-05-17T02:43:54+00:00

I have a bit of a problem. I have a java application and when

  • 0

I have a bit of a problem. I have a java application and when it starts it needs to make around six maybe seven threads that just wait for some sort of event to occur (i.e. user presses a button). An example of how I make these threads is below. The problem is, I open my task manager only to see that all my four cpu cores are at 100% (up from around 20), as soon as I close my application everything returns to normal. I am still new to multi-threading and I know I am committing some sort of concurrency sin here but I would appreciate any insight on how to rectify this situation.

new Thread (new Runnable() { public void run()
{
    while (true)
        if (doSomeFunction)
        {
            myFunction();
            doSomeFunction = false;
        }
} } ).start();

// Somewhere else in the code
doSomeFunction = true;

I am thinking perhaps wait and notify would be the right approch to doing this?

EDIT: Just for clarification, this has nothing to do with powering swing components. Instead this does events based on a script, I don’t want certain script functions to block but instead return immediately while finishing the task in the background.

  • 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-17T02:43:54+00:00Added an answer on May 17, 2026 at 2:43 am

    There are two possible answers here.

    The first is that you are waiting for a button press in a Swing application (or similar). If so, threads aren’t the answer. You should simply be using an action listener:

    JButton button = ...
    button.addActionListener(new ActionListener() {
      void actionPerformed(ActionEvent e) {
        // do stuff
      }
    });
    

    You need to be really careful about using threads in Swing applications. That’s because only one thread (called the Event Dispatching Thread or EDT) is the only thread allowed to make UI updates. All event handlers occur in the EDT. So while code is running on the EDT no UI updates will happen (moving/closing windows, pressing buttons, etc) so EDT code should be short-lived.

    The more general problem can be solved in a number of ways that depend on more information than is provided.

    Firstly, to answer your question: your CPU usage is going to 100% because the threads are constantly checking the value and calling a function in an endless loop.

    You should be using the Java 5+ concurrency model however, which will look something more like this:

    ExecutorService threadPool = Executors.newFixedThreadPool(10); // or whatever
    threadPool.submit(new Runnable() {
      public void run() {
        // do stuff
      }
    });
    

    As to how to do the inter-thread communication, that’s where it gets tricky. You’re sharing a primitive variable, which is a really bad idea.

    The simplest idiom is to use wait-notify:

    final Object someObject = ...
    ExecutorService threadPool = Executors.newFixedThreadPool(10); // or whatever
    threadPool.submit(new Runnable() {
      public void run() {
        synchronized (someObject) {
          someObject.wait();
        }
        // do stuff
      }
    });
    ...
    synchronized (someObject) {
      someObject.notify(); // wakes ONE waiting thread
    }
    

    The synchronized mutexes are really important.

    But Java 5 adds a whole bunch of alternatives to this. I would recommend buying Java Concurrency in Practice and reading it cover to cover.

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

Sidebar

Related Questions

So I have a bit of a performance problem. I have made a java
I have a bit of a problem that I can't seem to code my
I have a bit of a strange problem. I have a music app that
I have a problem modernizing a Java WebStart application under Java 6 u 13
I have some problem with my java application, I did build it with Eclipse
I have a stand-alone java application that makes some database read/write business on a
I am developing a client-side Java application that has a bit of functionality that
I have a Windows application based on Java, that I should like to install
I have a processing based Java application that runs fine in Eclipse but the
Hey I have a bit of a problem trying to locate my application in

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.