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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:27:22+00:00 2026-06-07T09:27:22+00:00

Possible Duplicate: How thread-safe is enum in java? Let there be an enum class

  • 0

Possible Duplicate:
How thread-safe is enum in java?

Let there be an enum class like

public enum Type
{
    ONE, TWO, THREE, FOUR
}

Is the Type.values() array thread safe to access in a for loop, e.g., in a static method? For example:

public static void process()
{
    for (Type type:Type.values())
    {
        // Do something
    }
}

Furthermore, if one defines a static array variable with any subset of these values, will that be thread safe to read? For example:

public static final Type[] TYPES = new Type[] {TWO, THREE};

public static void process()
{
    for (Type type:TYPES)
    {
        // Do something
    }
}

In both cases, the process() method may belong to a different class than the definition of the Type enum and of the types array.

Also, the TYPES array could be defined once in the Type enum and returned by a static method of that enum.

So, given the above, if multiple threads are running the process() method simultaneously, will the code be thread safe (at least for just reading the values of the TYPES array)?

  • 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-07T09:27:25+00:00Added an answer on June 7, 2026 at 9:27 am

    Others have already pointed out that the values() method returns a new array each time, so it is thread safe.

    However, to answer your second question:

    if one defines a static array variable with any subset of these values, will that be thread safe?

    No. Any public static array is not safe, because array elements are always mutable.
    That is, any thread could change TYPES[0] to be any other Type, which is not safe in the presence of multiple threads — or indeed even in a single-threaded program.

    Much better would be to expose a List<Type>, which can then be safely made immutable (e.g., using Collections.unmodifiableList()).

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

Sidebar

Related Questions

Possible Duplicate: thread with multiple parameters How does one thread a sub with two
Possible Duplicate: Java: “implements Runnable” vs. “extends Thread” When should you use: class MyThread
Possible Duplicate: Java: “implements Runnable” vs. “extends Thread” I have two questions about multithreaded
Possible Duplicate: Thread safety in Java class I'm reading Java concurrency in Practice ,
Possible Duplicate: Java Thread Garbage collected or not Consider the following class: class Foo
Possible Duplicate: Is stl vector concurrent read thread-safe? I have a multi-threaded program that
Possible duplicate question: Is there a way to indefinitely pause a thread? In my
Possible Duplicate: Malloc thread-safe? I am not a bit confused while I am reading
Possible Duplicate: .NET Controls: Why aren’t all calls thread-safe? This question is not about
Possible Duplicate: Does every thread need its own autorelease pool? I would like to

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.