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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:56:17+00:00 2026-06-06T05:56:17+00:00

I was just doing some random testing on locking in multi-threading this morning and

  • 0

I was just doing some random testing on locking in multi-threading this morning and strangely I found that locking a private “string” in two separate instance actually blocks the other thread from executing. Please find the code below for reference.

The thing that confuses me is that “string” in two objects are really two separate object, so why locking on one blocks the other? ( Note, if you replace string with other reference type object like List, it will not block the other thread from executing, which is really what we expected… )

class Program {
    static void Main(string[] args) {
        Thread th = new Thread(DoWork);
        th.Start();

        Thread th2 = new Thread(DoWork);
        th2.Start();
    }

    static void DoWork() {
        Test importer = new Test();
        importer.SyncTest();
    }



}

public class Test {
    public void SyncTest() {
        string find = "test";
        lock(find) {                
            Console.WriteLine("thread starting...");
            Thread.Sleep(4000);
        }
    }
}
  • 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-06T05:56:19+00:00Added an answer on June 6, 2026 at 5:56 am

    String constants are “interned”. This means that when you type:

    var s1 = "foo";
    var s2 = "foo";
    

    Both are the same instance of the string “foo”. Likewise, it’s the same instance when you invoke a method with a similarly defined local variable twice from different threads. This is done for performance reasons.

    This is a special case, but on the other hand, you really should not be locking on strings. (I’ve yet to see a situation where the idomatic solution of creating a new lock object is not the way to go — private object lockObject = new object();)

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

Sidebar

Related Questions

I've just started doing some real-world performance testing on my Fluent NHibernate / SQLite
I am doing some tests, just to learn (normally i do this in ajax
I've been doing some research and testing on how to do fast random selection
I am doing some performance testing on a SQL sproc and just want to
I'm basically just doing some tests to figure out a good way to write
So I'm working on just a learning project to expose myself to doing some
I have a progressBar using the ProgressBar class. Just doing this: progressBar = new
This is a very general question that is mostly just conceptual. I was thinking
expand.grid(i=rexp(5,rate=0.1)) It creates just one col but is there some way to multiply this
I have an animation of some parachutes that I'd like to add some random

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.