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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:23:00+00:00 2026-05-27T16:23:00+00:00

I am working on a project where one thread forks 4 other threads and

  • 0

I am working on a project where one thread forks 4 other threads and waits for their completion. The forking thread should have the ability to stop the forked threads at any time.
To, accomplish the same, I did something as such:

class ForkedThread implements Runnable {
    public boolean keepRunning = true;
    public void run() {
        while(keepRunning is true ){
        //Do the Job
        }
    }
}

Now since keepRunnig is public, it can accessed from outside and can be made false.
My Question is will I have to synchronize the usage of keepRunning or its fine.
Secondly, is this technique OK. Or shall I use a ThreadPool to do my job.
Actually, using ThreadPool will increase the complexity of my code, which I didn’t wanted. Please help.

  • 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-27T16:23:01+00:00Added an answer on May 27, 2026 at 4:23 pm

    Simply declare keepRunning as volatile, i.e.

    public volatile boolean keepRunning = true;
    

    and you’ll be fine. The volatile tells Java that the variable will be accessed by multiple threads and should be made thread-safe. This is not really a big issue with booleans, but becomes important with references and longs.

    The technique you’re using is good practice. It allows for graceful termination of the threads in question. I would recommend you look at the new concurrency libraries (i.e. ExecutorService) of Java as they makes things like you’re doing easier and safer as well. Well worth the investment!

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

Sidebar

Related Questions

I have a pet project that I'm working on that has multiple worker threads.
I am currently working on a project where one of our goals is to
I am only one person working on project - so I am developer without
I'm currently working on a project with a one page design that'll slide up
While working a project tonight, I ended up using one .js resource file for
The question really says it all. For one project I am working on, the
I'm working on a project which stores single images and text files in one
My team is working on a conversion project to convert one product (but with
The worst thing when working on a one man project is the lack of
I am working in a project where there are configuration files, one in number

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.