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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:22:17+00:00 2026-05-11T13:22:17+00:00

The following program prints: Entered 3 Entered 4 Wait for Exited messages Exited 3

  • 0

The following program prints:

Entered 3 Entered 4 Wait for Exited messages Exited 3 Exited 4 

Meaning that it cannot acquire an exclusive lock on resource. Why?

public class Worker     {         public void DoIt(object resource)         {             Monitor.Enter(resource);             Console.WriteLine('Entered ' + Thread.CurrentThread.ManagedThreadId);             Thread.Sleep(3000);             Monitor.Exit(resource);             Console.WriteLine('Exited ' + Thread.CurrentThread.ManagedThreadId);         }     }      class Program     {         struct Resource         {             public int A;             public int B;         }          static void Main(string[] args)         {             Resource resource;             resource.A = 0;             resource.B = 1;              var a = new Worker();             var b = new Worker();              var t1 = new Thread(() => a.DoIt(resource));             var t2 = new Thread(() => b.DoIt(resource));              t1.Start();             t2.Start();              Console.WriteLine('Wait for Exited messages');             Console.ReadLine();         }     } 
  • 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-11T13:22:18+00:00Added an answer on May 11, 2026 at 1:22 pm

    Your Resource is a struct. It is boxed when passed to DoIt, so each call to DoIt locks a different object. Change Resource to be a class.

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

Sidebar

Related Questions

Due to a bug that was fixed in C# 4, the following program prints
I have a terribly uncomplicated test program that prints out the following numbers. i.e.
The following program prints A:C(A,B) B:C(A,B) (as it should) public interface I { string
Why the following program prints B B (as it should) public class A {
let us have a situation in which the following program prints some 10 lines
Consider the following program: $x=12345678901.234567000; $y=($x-int($x))*1000000000; printf(%f:%f\n,$x,$y); Here's what is prints: 12345678901.234568:234567642.211914 I was
In the following code, am executing a java program Newsworthy_CA. The program prints the
The following program : public class SimpleCounter extends HttpServlet { int counter=0; @Override protected
can anyone tell me whats wrong in the following program that accepts 1 or
Why following program every time prints I'm string and not I'm object. or I'm

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.