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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:38:16+00:00 2026-05-22T11:38:16+00:00

Im trying to figure out the output of this code: Dictionary<int, MyRequest> request =

  • 0

Im trying to figure out the output of this code:

Dictionary<int, MyRequest> request = new Dictionary<int, MyRequest>();

for (int i = 0; i < 1000; i++ )
{
  request.Add(i, new MyRequest() { Name = i.ToString() });
}

var ids = request.Keys.ToList();

Parallel.For(0, ids.Count, (t) =>
{
  var id = ids[t];
  var b = request[id];

  lock (b)
  {
    if (b.Name == 4.ToString())
    {
      Thread.Sleep(10000);
    }

    Console.WriteLine(b.Name);
  }
});

Console.WriteLine("done");
Console.Read();

output:

789
800
875
.
.
.
4
5
6
7
done

MyRequest is just a dummy class used for demonstration (it is not doing anything but holding values). Is my lock blocking the execution or are the last 4 being put on their own thread?

This is a .NET 4.0 demo.

UPDATE
Ok I did figure out they were on teh same thread, but i would still like to know if the lock does anything to block execution. I cant imagine it does.

  • 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-22T11:38:17+00:00Added an answer on May 22, 2026 at 11:38 am

    If ids does not contain duplicates, that lock won’t block anything. But if there are duplicates in ids, then yes, there might be contention at the lock, as different threads fight for access to the same request.

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

Sidebar

Related Questions

Trying to figure out this pseudo code. The following is assumed.... I can only
I wrote this code today, just out of experimentation, and I'm trying to figure
I'm trying to figure out the answer to this question with the following code,
I am trying to figure out .net and got this code that when I
UPDATE - New code at the bottom I'm trying to figure out how to
i was trying to figure out what is wrong with this piece of code,
I'm trying to figure out how to redirect output from some FORTRAN code for
I'm having a hard time trying to figure out why this piece of code
Trying to figure out the best way to do this: Should I do something
In trying to figure out this problem (which is still unsolved and I still

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.