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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:42:09+00:00 2026-05-14T23:42:09+00:00

I have developed a Java Swing application, which uses the SwingWorker class to perform

  • 0

I have developed a Java Swing application, which uses the SwingWorker class to perform some long running tasks. When the application is run from the IDE (Netbeans), I can start multiple long running tasks simultaneously without any problem.

I created a runnable jar file for the application, in order to be able to run it from outside the IDE. The application when run from this jar file works well with the only exception that it doesn’t allow me to start 2 long running tasks simultaneously. The tasks just run one after the other.

I managed to create a very simple program which demonstrates this problem. link
The program uses a swingworker which just loops from 1 till 100 and writes the number to the console. The two buttons initiate two threads which do the same thing. If i run this program on netbeans, the threads interleave, while if i create a jar file and run the application from the jar file, the threads do not interleave, but run one after the other.

It seems as if the jvm does not allow more than one thread to run at any one time, when the application is run from the jar file.

Here is the code for those of you having problems with the link

package testingjarpath;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class Main extends JFrame {
    private JButton btnTest;
    private JButton btnTest2;

    public Main() {

        this.btnTest = new JButton("Test 1");
        this.btnTest.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new Main.MyTask("First").execute();
            }
        });

        this.btnTest2 = new JButton("Test 2");
        this.btnTest2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new Main.MyTask("Second").execute();
            }
        });

        this.setLayout(new FlowLayout());
        this.add(this.btnTest);
        this.add(this.btnTest2);
        this.setSize(new Dimension(400, 400));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Main();
            }
        });
    }

    public class MyTask extends SwingWorker<Void, Integer> {
        private String str;

        public MyTask(String str) {
            this.str = str;
        }

        @Override
        protected Void doInBackground() throws Exception {
            for (int i = 0; i < 100; i++) {
                Thread.sleep(100);
                publish(i);
            }
            return null;
        }

        protected void process(List<Integer> progress) {
            System.out.println(str + " " + progress.get(progress.size() - 1));
        }

        @Override
        protected void done() {
            System.out.println(str + " is ready");
        }

    }

}

Thanks in advance,
Peter Bartolo

  • 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-14T23:42:09+00:00Added an answer on May 14, 2026 at 11:42 pm

    Apparently, SwingWorkers by default all execute on the same background thread in JDK 1.6

    Add these

    import java.util.concurrent.Executor;
    import java.util.concurrent.Executors;
    

    At the top of your Main() add this

    final Executor executor = Executors.newCachedThreadPool();
    

    And in your actionPerformeds, execute your SwingWorkers like this

    executor.execute(new Main.MyTask("First"));
    

    This will execute each SwingWorker on a separate thread in a Thread pool.

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

Sidebar

Related Questions

I have developed a java application which run's perfectly in local server. But When
I have developed a java swing client-server application. The server has many services like
I have developed a Java Swing application, and it will be installed on multiple
I have developed java application which is actually stored at d drive.I have imported
I have developed a standalone application using java, in which I am executing an
I have developed a java application which takes a screenshot using robot (presses Print
I have a Java Swing application, developed on Mac OS X 10.5 using Java
I have developed Java desktop application using Netbeans. In my application, I used some
I have a stand-alone Java windows application developed based on Swing. It connects to
I have already developed a JAVA SWING Application . I want to install this

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.