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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:58:15+00:00 2026-06-15T08:58:15+00:00

I’m writing a Java application that uses some threads. Main method looks like this:

  • 0

I’m writing a Java application that uses some threads. Main method looks like this:

WorkerThread[] thread = new WorkerThread();
for (int i = 0 ; i<10 ; i++) {
    thread = new WorkerThread();
    thread.doStuff();
}

and my WorkerThread looks like this:

class WorkerThread {
    public void run() {
        ...
    }

    public void doStuff() {
        ...
    }
}

The matter is that, obviousely, I call doStuff from the run method it is executed like concurrently with other threads, but if i call doStuff directly from the main it is executid not cocurrently.

Ok, now the question is: is there a way to execute, concurrently, the doStuff method calling it not from the run method, but rather, from the main?

  • 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-06-15T08:58:17+00:00Added an answer on June 15, 2026 at 8:58 am

    You cannot directly call methods on a Thread object. The only way to get concurrency with a Thread is to invoke its start() method, which in turn calls run() on a separate thread of execution. Calling any other methods directly from main() is just like calling methods on regular non-Thread objects; there’s no concurrency, nothing special happens, the methods execute immediately.

    It sounds like you’re trying to communicate between the main thread and the worker threads. Each thread’s run() method is in charge of executing code on that thread. If you can communicate with that run() method then you can control what happens on that thread.

    One way to do that is to pass a variable in when you construct the Thread. Save a value that the run() method can examine to determine what to do. For example, below we have an enum that has three possible actions in it. The run() method looks at the enum and decides what to do based on the value that was passed to the WorkerThread() constructor.

    class WorkerThread {
        public enum Action { DO_STUFF, MAKE_WIDGETS, FROB_BARS }
        private Action action;
    
        public WorkerThread(Action action) {
            this.action = action;
        }
    
        public void run (){
            switch (action) {
                case DO_STUFF:     doStuff();     break;
                case MAKE_WIDGETS: makeWidgets(); break;
                case FROB_BARS:    frobBars();    break;
            }
        }
    
        public void doStuff()     { ... }
        public void makeWidgets() { ... }
        public void frobBars()    { ... }
    }
    

    Now our main() method looks like this. You create WorkerThreads passing in the action to perform, then call start().

    WorkerThread[] threads = new WorkerThread[10];
    for (int i = 0; i < threads.length; ++i) {
        threads[i] = new WorkerThread(WorkerThread.Action.DO_STUFF);
        threads[i].start();
    }
    

    You could do the same thing in myriad different ways. Instead of having one WorkerThread class you could have several different classes with differing run() methods and then instantiate the appropriate sub-class from main().

    class DoStuffWorkerThread     extends Thread { ... }
    class MakeWidgetsWorkerThread extends Thread { ... }
    class FrobBarsWorkerThread    extends Thread { ... }
    

    You can even create anonymous Thread classes right inside main(), if you like. Then the logic can be whatever main() wants.

    Thread[] threads = new Thread[10];
    for (int i = 0; i < threads.length; ++i) {
        threads[i] = new Thread() {
            public void run() {
                doStuff();
            }
        };
    
        threads[i].start();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

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.