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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:01:10+00:00 2026-05-25T12:01:10+00:00

I want to make a thread, which runs, computes something with the data i

  • 0

I want to make a thread, which runs, computes something with the data i give it, and returns a few values, or an object. The thread is a part of a Swing GUI.

My question: How can I make a method that runs when I make the thread, and returns an object (or whatever I want it to return)?

My code:

private void nextTurn () {
    // do something
    if (turn == white) {
        try {
            Engine e = new Engine(); // Engine is implemented by runnable
            e.start();
            Move m = e.getBestMove (board);
            // thread should work, next code should be excecuted immediately
        }
        catch (Exception e) {}
    }

    // end of Main class
}

This is the first time I am working with Threads, and I know you should avoid them if possible, but I need it this time for my GUI.
The info on the Oracle site on Threads did not help me out. I am able to make a program with multiple Threads that runs indefinately, but I can’t make it work with functions.

  • 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-25T12:01:11+00:00Added an answer on May 25, 2026 at 12:01 pm

    Since this is with a Swing GUI, consider using a SwingWorker object which creates a background thread (all the code run in the doInBackground method), and then can return a final result and/or interim results. Information on how to use this is well documented in the tutorials here:

    Concurrency in Swing

    SwingWorkers have property change support and thus will allow listeners to observe its state (as a SwingWorker.StateValue) via a PropertyChangeListener. This is one way your program can determine that the thread has completed its processing, get the returned result and go from there.

    On an unrelated note, this isn’t in your production code is it?:

    catch (Exception e) {}
    

    If so, you will likely want to fix this as ignored exceptions can bite you in the tail big time.

    e.g.,

      if (turn == white) {
         try {
            final SwingWorker<Move, Void> mySwingWorker = new SwingWorker<Move, Void>() {
               @Override
               protected Move doInBackground() throws Exception {
                  Engine e = new Engine(); // Engine is implemented by runnable
                  e.start();
                  Move m = e.getBestMove(board);                  
                  return m;
               }
            };
    
            mySwingWorker.addPropertyChangeListener(new PropertyChangeListener() {
               public void propertyChange(PropertyChangeEvent evt) {
                  if (StateValue.DONE == mySwingWorker.getState()) {
                     try {
                        Move m = mySwingWorker.get();
    
                        // TODO: insert code to run on the EDT after move determined
    
                     } catch (InterruptedException e) {
                        e.printStackTrace();
                     } catch (ExecutionException e) {
                        e.printStackTrace();
                     }
                  }
               }
            });
            mySwingWorker.execute();
    
         } catch (Exception e) {
            e.printStackTrace();
         }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make one thread that puts values to a queue when it
I want to make a program in java in which one thread is reading
I want to make a copy of an ActiveRecord object, changing a single field
I have a Java Thread which exposes a property which other threads want to
I have the following piece of code, which I want to make parallel in
What I want to do : run a background thread which calculates ListView contents
I have a thread which performs some tasks. At the end I want to
I want to make my class thread-safe without large overhead. The instances will be
What I want in short is: Core Data that runs without blocking the main
I have just started Android Development, I want to make thread in my application

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.