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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:05:13+00:00 2026-05-11T10:05:13+00:00

See this code: public class multiply { public Thread myThread; public int Counter {

  • 0

See this code:

public class multiply {     public Thread myThread;     public int Counter     {         get;         private set;     }     public string name     {         get;         private set;     }      public void RunConsolePrint()     {          lock(this)         {         RunLockCode('lock');         }       }      private void RunLockCode(string lockCode)     {         Console.WriteLine('Now thread '+lockCode+' ' + name + ' has started');         for (int i = 1; i <= Counter; i++)         {             Console.WriteLine(lockCode+' '+name + ': count has reached ' + i + ': total count is ' + Counter);         }         Console.WriteLine('Thread ' + lockCode + ' ' + name + ' has finished');     }     public multiply(string pname, int pCounter)     {         name = pname;         Counter = pCounter;         myThread = new Thread(new ThreadStart(RunConsolePrint));     }  } 

And this is the test run code:

    static void Main(string[] args)     {         int counter = 50;          multiply m2 = new multiply('Second', counter);         multiply m1 = new multiply('First', counter);         m1.myThread.Start();         m2.myThread.Start();         Console.ReadLine();     } 

I would expect that m2 must execute from start to finish before m1 starts executing, or vice versa, because of the lock statement. But the result I found was the call to lock first and lock second was intermingled together, i.e., something like this

Now thread lock First has started Now thread lock Second has started lock First: Count has reached 1: total count is 50 lock First: Count has reached 2: total count is 50 lock Second: Count has reached 1: total count is 50 

What did I do wrong?

  • 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. 2026-05-11T10:05:13+00:00Added an answer on May 11, 2026 at 10:05 am

    Each instance of the code is locking on a different object. Your lock object needs to be shared between all instances — make it a static class variable.

    private static object syncRoot = new object(); public void RunConsolePrint() {     lock(syncRoot)     {          RunLockCode('lock');     }     } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 145k
  • Answers 145k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is never any harm in keeping as much information… May 12, 2026 at 8:54 am
  • Editorial Team
    Editorial Team added an answer In this example f is not the @applicant object, it… May 12, 2026 at 8:54 am
  • Editorial Team
    Editorial Team added an answer Looks like this problem is due to the default browser… May 12, 2026 at 8:54 am

Related Questions

If I am using ReaderWriterLockSlim to acquire read/write locks, do I need to make
I'm having an issue with pulling a Spring bean from an application context. When
So I've seen a lot of articles now claiming that on C++ double checked
Context: I need to develop a monitoring server that monitors some of our applications

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.