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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:48:43+00:00 2026-05-18T06:48:43+00:00

I am struggling to understand on java threads work so excuse this rather simple

  • 0

I am struggling to understand on java threads work so excuse this rather simple question.

Let’s assume I have a program with N threads. Each thread executes the same instructions on a different part of an array of Strings. We invoke the thread through a class with a runnable interface. For the purposes of this example, let say it is something like this:

run() {
    while (startStop = loopGetRange() != null) {

        countLetters(startStop.start,startStop.stop);
        /* start is the beginning cell in the array where the process starts
          and stop is the ending cell in the array where the process stops */
    }
}

Finally countLetters is just a simple method as follow:

private void countLeters (int start, int stop) {
    for (int y = start; <= stop; y++) {
        String theWord = globalArray[y];
        int z = theWord.length;
        System.out.println("For word "+theWord+" there are "+z+" characters");
    }
}

Here is my question: Are variables like “theWord” and “Z” local to the thread or are they shared across the thread and are thus subject to possible thread collisions. If the latter, how best to protect these variables.

Thanks for helping a confused person.

Elliott

  • 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-18T06:48:44+00:00Added an answer on May 18, 2026 at 6:48 am

    Local variables are allocated on the stack, and are local to the thread. Only member fields are shared across threads. So, theWord and Z are not shared across threads and you don’t need to worry about clashing.

    Given that a String is immutable, the only concern about thread safety we would have in method countLeters() is access to the globalArray.

    Now, if the construction of this array “happened-before” the access to globalArray, the code is safe as long as no thread “writes” to the globalArray.

    “happened-before” relationships can be enforced by number of ways (by using the synchronized keyword, final keyword, volatile keyword, using java.util.concurrent libraries etc.).

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

Sidebar

Related Questions

I'm struggling to understand Dependency Properties in Silverlight 2. Does anybody have a good
I have for the last several years been struggling to understand why the Internet
Still struggling to understand what best practices are with respect to macros. I'm attempting
I'm struggling to understand something about GIT. We've got a repository with a number
I'm struggling to find the right terminology here, but if you have jQuery object...
A coworker has been struggling with this problem. The desired result is an installable
I am struggling with the route setup for a Rails application. I have installed
I'm working on a Java Swing application. I have a button whose action runs
I'm struggling to understand what you would get from the Royal Mail if you
I want to write a very simple implementation of an onion router in Java

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.