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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:44:12+00:00 2026-05-30T03:44:12+00:00

class Class1 { private static object consoleGate = new Object(); private static void Trace(string

  • 0
class Class1
    {
        private static object consoleGate = new Object();

        private static void Trace(string msg)
        {
            lock (consoleGate)
            {
                Console.WriteLine("[{0,3}/{1}]-{2}:{3}", Thread.CurrentThread.ManagedThreadId,
                    Thread.CurrentThread.IsThreadPoolThread ? "pool" : "fore",
                    DateTime.Now.ToString("HH:mm:ss.ffff"), msg);
            }
        }
        private static void ProcessWorkItems()
        {
                    lock (consoleGate)
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            Trace("Processing " + i);
                            Thread.Sleep(250);
                        }
                    }
            Console.WriteLine("Terminado.");

        }
        static void Main()
        {
            ProcessWorkItems(); Console.ReadLine();
        }
    }

output:

Processing 0
Processing 1
Processing 2
Processing 3
Processing 4
Terminated

Why is this code works? ProcessWorkItems static method locks ConsoleGate object and Trace did the same. I thought the object could only be locked once. ¿Some explanations?

  • 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-30T03:44:13+00:00Added an answer on May 30, 2026 at 3:44 am

    locks in C# are re-entrant – a single thread can acquire the same lock multiple times without blocking. Sine you only have one thread here there is no problem – locks are for synchronizing access to resources across multiple threads.

    From the MSDN documentation on lock:

    The lock keyword ensures that one thread does not enter a critical
    section of code while another thread is in the critical section. If
    another thread tries to enter a locked code, it will wait, block,
    until the object is released.

    For more information on re-entrant-locking see this SO thread: “What is the Re-entrant lock and concept in general?”

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

Sidebar

Related Questions

public class SelfCallingTest { private static int counter; public void SelfCallingMethod(int counter) { Console.WriteLine(The
Consider the following code snippet within a class private static Object _syncroot = new
public class StaticTest { private static StaticTest stObj=new StaticTest(); private static int VAR1=10; private
public class Readparam { private static String method_name; public static HashMap<String, Vector<String>> getParameters(String file_name)
Consider this sample class, class TargetClass { private static String SENSITIVE_DATA = sw0rdfish; private
public abstract class ExeCommand { private static object commandHandler; public static object CommandHandler {
I have this class: private static class ClassA{ int id; String name; public ClassA(int
If I use SMTPTransport ( http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/SMTPTransport.html ) as private static object for class that
In PHP, I have following Singleton Database Class: class Database { private static $instance;
I have this class: public static class CsvWriter { private static StreamWriter _writer =

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.