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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:41:16+00:00 2026-05-17T17:41:16+00:00

I wanted to do some testing with queues and using Queue.Synchronized to see how

  • 0

I wanted to do some testing with queues and using Queue.Synchronized to see how it worked out when having two threads putting stuff into a queue, and a third thread reading it out. It seemed fairly simple, but it’s not working right and I’m not sure why. Probably a silly mistake on my part. There’s a bit more to it though.

class Program
{
    public int this1 = 0;
    static void Main(string[] args)
    {
        Tester test1 = new Tester();

        Queue myQ = new Queue();
        Tester.myQ = Queue.Synchronized(myQ);
        Thread test1_thread = new Thread(new ThreadStart(test1.test1));
        Thread test1_thread2 = new Thread(new ThreadStart(test1.test1));

        test1_thread.Start();
        test1_thread2.Start();

        int i = 0;
        while (i <= 10)
        {

            i++;
        go_back_here:
            try
            {
                Tester.myQ.Enqueue(40);
                Console.WriteLine("did it");
                int holding = Convert.ToInt32(Tester.myQ.Dequeue());
                Console.WriteLine("reading out {0}: {1}", i);
            }
            catch
            {
                //Console.Write("e");
                //goto go_back_here;
            }
        }
        Console.ReadLine();
    }


}

class Tester
{

    public static Queue myQ;
    public void test1()
    {
        int this1 = 0;

        while (this1 <= 10)
        {
            Console.WriteLine("Doing something {0}", this1);
            myQ.Enqueue(this1);
            Console.WriteLine("(peek) {0}", myQ.Dequeue());
            this1++;
        }

    }
}

Through my own testing I’ve found that the two Tester threads are loading stuff into the queue fine. When my main loop would try to Dequeue it, I would get an error saying the queue was empty. I put peeks and then Dequeues in the Testing threads and they were able to show the stuff in the thread fine.

Then I wondered if I was somehow accessing the wrong Queue from the main loop (OOP is not my forte, still learning it), so I added “Tester.myQ.Enqueue(40);” to see if I could stick something in it. My peeks/Dequeues didn’t show it, but, it DID show up when I added the “did it” write line. I’ve run the program a ton of times and the 40 from the main loop only appears in the Testing threads’ printing out of Dequeue when the “did it” is there. Never when I comment it out.

Could someone more knowledgeable about the subject shed some light on what’s going on? (and yes, I know my use of a goto is a terrible thing, I let it slide because this was supposed to be a simple test)

  • 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-17T17:41:17+00:00Added an answer on May 17, 2026 at 5:41 pm

    It seems that your initial problem was that your main thread was accessing the queue too soon (nothing in it because the threads where starting to put something in it at that same time). You will need to ensure that the threads have finished working before reading from the queue in the main thread. You can wait for them by calling Thread.Join or, alternatively, Thread.Sleeping in a loop while checking for the condition of some global variable set by the threads. It’s not enough to have a while loop without sleep or join.

    Furthermore, your “peek” isn’t really a peek because it actually removes the item. You need to use Console.WriteLine("(peek) {0}", myQ.Peek()); rather than myQ.Dequeue().

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

Sidebar

Related Questions

I am developing a app using Monodroid (C#) and wanted to some good looking
I've made a simple dummy server/dummy client program using IOCP for some testing/profiling purpose.
I have done some testing but I wanted to ask if anyone sees a
I wanted to play around with Linux and do some testing. I've tried installing
In some of my integration tests I start multiple threads and wanted to ensure
I need to do some testing for a website made in php/mysql and wanted
Just started testing nodejs, and wanted to get some help in understanding following behavior:
I wanted some system where I didn't have to keep looking at the Exception
I would like to learn ASP.NET and just wanted some input as to which
I know the topic I started is too subjective. But I just wanted some

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.