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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:03:15+00:00 2026-05-27T11:03:15+00:00

I need to create 5 threads and associate an ArrayList with each thread .I

  • 0

I need to create 5 threads and associate an ArrayList with each thread .I have another thread which will read values from a queue (one by one) and push that message to the
ArrayList associated with each thread which i created earlier . Then that thread should read value from the ArrayList and start executing. How can I do that?

  • 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-27T11:03:15+00:00Added an answer on May 27, 2026 at 11:03 am

    You can use the Monitor class to synchronize your threads. Here is an example using five different locks and queues, one for each thread, based on your comments.

    It’s important to protect with a lock any data shared between two different threads. Only the thread holding the lock should access the data.

    The worker thread locks on its own personal object (in the syncs array), and, having the lock, then calls Monitor.Wait, which will release the lock.

    The main thread may have already tried to lock that thread’s sync object, or soon will, it doesn’t matter because it won’t have access to that thread’s queue until it does have the lock. Then it is safe to queue up a message. The Monitor.Pulse call wakes up the waiting worker thread, but the worker stays stuck in its Monitor.Wait call until the main thread releases the lock (by falling out of the lock() { } code block.)

    When Monitor.Wait returns to the worker, the lock will have been re-acquired.

    You can’t always assume that the threads will pulse and wake at the same rate, which is why I have the extra while loop in the worker thread, to handle the case where the main thread has signaled a few times with several messages before the worker actually woke up to process them.

    This example is much simplified — it doesn’t cover shutting the workers down, for example, but it should give you some ideas get started.

    namespace ConsoleApplication1
    {
        using System;
        using System.Collections.Generic;
        using System.Threading;
    
        internal class Program
        {
            private static readonly Queue<int>[] queues = new Queue<int>[5];
    
            private static readonly object[] syncs = new object[5];
    
            public static void Main(string[] args)
            {
                for (int i = 0; i < 5; i++)
                {
                    queues[i] = new Queue<int>();
                    syncs[i] = new object();
                    var thread = new Thread(ThreadProc);
                    thread.Start(i);
                }
    
                var random = new Random();
                while (true)
                {
                    Thread.Sleep(1000);
                    int index = random.Next(queues.Length);
                    lock (syncs[index])
                    {
                        int message = random.Next(100);
                        queues[index].Enqueue(message);
                        Console.WriteLine("Sending message " + message + " to thread at " + index);
                        Monitor.Pulse(syncs[index]);
                    }
                }
            }
    
            private static void ThreadProc(object data)
            {
                var index = (int)data;
                lock (syncs[index])
                {
                    while (true)
                    {
                        while (queues[index].Count == 0)
                        {
                            Monitor.Wait(syncs[index]);
                        }
    
                        int message = queues[index].Dequeue();
                        Console.WriteLine("Thread at " + index + " received message " + message);
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need to create multiple processing threads in a new application. Each
Need to create a custom DNS name server using C which will check against
In web.py i need to create a shared variable, for which multiple threads(requests) can
I need to create a pool of socket connections which will be served to
i create three threads in my main program. i have for loop in each
I need to create a historical timeline starting from 1600's to the present day.
I need to create an ASP page (classic, not ASP.NET) which runs remote shell
I need to create multiple threads when a button is clicked and i've done
I need to create a function which appends a value to a vector and
I need to create an out-of-process COM server (.exe) in C# that will be

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.