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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:54:16+00:00 2026-05-23T19:54:16+00:00

I have a one String which will be access by four different threads in

  • 0

I have a one String which will be access by four different threads in unpredicatable order.

String s = "When value is";

Now above string s will be updated by four different threads through Java Swing EDT. I have two JCombobox and two JTextField. Inside event handler of these components I will have to update above string.

For e.g.
when textfield focus changes string should be “When value is X”
when combo changes string should be “When value is less than X and Y”

So like above four thread will be changing one string. If I dont control them output is unexpected. What is the best way to solve this. I can use join() or may go for volatile but it will make code complex.

Please share your ideas. Thanks in advance.

  • 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-23T19:54:16+00:00Added an answer on May 23, 2026 at 7:54 pm

    Firstly, a String is immutable so it cannot be updated. Another object, such as a Swing component, may however hold a reference to a String, and this reference can be updated to point to a different String. This is what normally happens when text is updated in a GUI.

    If you have four different threads that need to update a Swing component, e.g. to display a different String, they should do it by queuing a task to be run on the single Event Dispatching Thread using the SwingUtilities methods invokeLater or invokeAndWait, e.g.

    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        myComponent.setText("New string");
      }
    };
    

    Or in a more sophisticated application you might want to update a domain object, which would then need to be thread safe, from each of these four threads, and then separately update the Swing component on the EDT. Creating thread-safe code is not easy, in my opinion. I highly recommend the book Java Concurrency in Practice, but the main point is to ensure that only one thread at a time carries out any operation that involves the object in question being temporarily in an inconsistent state. This is sometimes possible by suitable use of existing thread-safe classes and atomic operations, but may require the use of a lock, typically through synchronized methods or blocks.

    Of course, none of this is relevant if, as it now appears, you do not have four threads at all but only four event handlers called on the EDT.

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

Sidebar

Related Questions

I have an client app which will use a connection string to get access
I have a function which parses one string into two strings. In C# I
I have one method in my managed bean which returns javascript as a string.
I would like to have a mapping which maps two string into one string.
I have a WCF service which accepts a string as a paramter for one
I have a function written in C# which has one parameter type as Dictionary<string
I'm using .NET 3.5. I have two string arrays, which may share one or
I have two strings, one a key and one a value, which I would
Imagine I have an function which goes through one million/billion strings and checks smth
I have one string name as id in my .aspx.cs page and one JavaScript

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.