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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:53:32+00:00 2026-05-19T15:53:32+00:00

I have a very simple application with a very strange behaviour. It’s essentially the

  • 0

I have a very simple application with a very strange behaviour.

It’s essentially the SwingWorker example, but when I press the button the GUI behaves just like the EDT is being blocked. I can fire off two simultaneously and they run in parallel (have near identical run times) but still the menu freezes up while they’re running. The exact same behaviour happens when I use a Thread with a runnable. Also interesting is that the GUI behaves properly if the loops are replaced by a Thread.sleep.

Any ideas?

public class DummyFrame extends JFrame {

        public DummyFrame() {
                JMenuBar bar = new JMenuBar();
                JMenu menu = new JMenu("File");
                menu.add(new JMenuItem("TEST1"));
                menu.add(new JMenuItem("Test2"));
                bar.add(menu);
                setJMenuBar(bar);

                JButton button = new JButton("FOOBAR");
                button.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                                final long start = System.currentTimeMillis();

                                SwingWorker<Void, Integer> testTask = new SwingWorker<Void, Integer>() {        
                                        @Override
                                        protected Void doInBackground()
                                        throws Exception {
                                                int k = 0;
                                                for (int i=0; i<200000; i++) {
                                                        for (int j=0; j<100000; j++) {
                                                                if (i==j && i%10000 == 0)
                                                                        k++;
                                                        }
                                                }
                                                System.out.println(k+" "+(System.currentTimeMillis()-start));
                                                return null;
                                        }
                                };
                                testTask.execute();
                        }

                });
                getContentPane().add(button);
                pack();
        }

        public static void main(String[] args) {
                SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                                DummyFrame f = new DummyFrame();
                                f.setVisible(true);     
                        }
                });
        }
}
  • 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-19T15:53:33+00:00Added an answer on May 19, 2026 at 3:53 pm

    The problem is in the VM’s implementation of threads. The specification does not specify how this is to be done. Java threads are supposed to map to native Windows threads and then use the Windows scheduler to share time slices. It’s unclear if this is exactly going on, and all of the official documentation only supports thread information running on Solaris.

    I believe the main problem comes from implementation details for preemption of threads. Probably this is being caused by some combination of code optimization on compiling and preemption control between the JVM and the native OS. JVM’s can use method calls as points to preempt threads, and I think part of the problem here is the two loops that you’re calling one on top of the other. If you break them up with a function call, it performs much better on my machine. I’m using 1.6.0_23 64-Bit Server VM on Windows 7.

    SwingWorker<Void, Integer> testTask = new SwingWorker<Void, Integer>() {
    
        private int k;
    
        private void inc() {
            this.k++;
        }
    
        private void innerLoop(int i) {
            for (int j=0; j<100000; j++) {
                if (i==j && i%10000 == 0)
                    this.inc();
            }
        }
    
        @Override
        protected Void doInBackground()
        throws Exception {
            System.out.println("Started");
            for (int i=0; i<200000; i++) {
                this.innerLoop(i);
            }
            System.out.println(k+" "+(System.currentTimeMillis()-start));
            return null;
        }
    };
    

    Even this has problems after starting several of them at once, however. The best solution is to add a call to Thread.yield() each time you start the inner loop. This ensures that the compiled code gives the scheduler an opportunity each iteration to preempt the thread.

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

Sidebar

Related Questions

I have a very simple .net application for testing SMTP on .net. But i
Very simple Qt GUI application: On the scene I have multiple circles implemented as
I have encountered something very strange, simple WPF application <Window x:Class=ListBoxSelection.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow
We have a very strange behaviour of a WPF application. Sometimes (not very ofen),
I have a rather strange problem. I have a very simple application that reads
I have a very simple application that works but when i add roboguice it
I have a very simple application test in which I want to drag and
I have a very simple application and I cannot make the WebView to show
I have written very very simple console application which supports some command line options.
I have written a very simple application to update my twitter status on a

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.