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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:21:55+00:00 2026-06-08T10:21:55+00:00

In this multi-thread program I’m trying to remove two random elements from a list

  • 0

In this multi-thread program I’m trying to remove two random elements from a list every third iteration of populating it. I thought I could do it by calculating the modulo of a “stepCounter” to keep track of how many times new elements were being added to the list (All the threads perform the same function) and after checking with a few breakpoints, I found that it is checking the stepCounter condition, then going to the first line of the “for” loop to pick two random indexes to remove.

The problem is that the removing is not actually being performed, as though it reads the “for i = etc. and then just skips over.

namespace ThreadWinApp
{
public partial class Form1 : Form
{
// thread variables is created here for the whole class
private Thread trd;
private Thread trd2;
private Thread trd3;

    //this locker object is used to sychronize the threads
    private System.Object locker = new System.Object();

    //random value object for use in all threads
    Random random = new Random();
    public int val;
    private int stepCounter = 0;

    //our "array" of integers undefined in size
    private List<int> numList = new List<int>();
    private int[] arry;



    private void ThreadTask()
    {

      //this the thread that will at random intervals add a random number to the collection. 
     // Every third iteration, the thread should remove two random 
     // elements from the collection.
        int randomVal;
        int randomListVal;
       // int stepCounter = 0;


        while (true)
        {
            lock (locker)
            {
                randomVal = random.Next(0, 20);
                numList.Add(randomVal);
                stepCounter += 1;

                if (((stepCounter % 3) == 0))

                    for (int i = 0; i == 2; i++)
                    {
                     randomListVal = random.Next(0, numList.Count);
                     numList.RemoveAt(randomListVal);

                    }

                Thread.Sleep(randomVal * 100);              
            }

        }

        }
    private void ThreadTask2()
    {

        //this the thread that will read and show data from the array created in ThreadTask2()
        // at random intervals add a random number to the collection. 
        // Every third iteration, the thread should remove two random 
        // elements from the collection.

        int randomVal;
        int randomListVal;
    //    int stepCounter = 0;

        while (true)
        {
            lock (locker)
            {
                randomVal = random.Next(0, 20);
                numList.Add(randomVal);
                stepCounter += 1;

                if (stepCounter % 3 == 0)
                {
                    for (int i = 0; i == 2; i++)
                    {
                      randomListVal = random.Next(0, numList.Count);
                      numList.RemoveAt(randomListVal);
                    }
                }

                Thread.Sleep(randomVal * 100);

            }
        }
    }

         private void ThreadTask3()
    {

      //this the thread that will read and show data from the array created in ThreadTask2()
     // at random intervals add a random number to the collection. 
     // Every third iteration, the thread should remove two random 
     // elements from the collection.
        int randomVal;
        int randomListVal;




        while (true)
        {
            lock (locker)
            {

                randomVal = random.Next(0, 20);
                numList.Add(randomVal);
                stepCounter += 1;



                if (stepCounter % 3 == 0)
                {
                    for (int i = 0; i == 2; i++)
                    {
                        randomListVal = random.Next(0, numList.Count);
                        numList.RemoveAt(randomListVal);
                    }
                }

                Thread.Sleep(randomVal * 100);

            }

        } 
        }


         private void ThreadTask4()
         {
             int[] array;

             while (true)
             {
                 lock (locker)
                 {

                     array = numList.ToArray();
                     for (int i = 0; i < array.Length; i++)
                     {
                         textBox1.Text += (array[i] + " ");
                     }

                     Thread.Sleep(1000);
                     textBox1.Text += ("\r\n");

                 }

             }
         }
    private void Form1_Load(object sender, EventArgs e)
    {

        //creates a new instance of thread1
        Thread trd = new Thread(new ThreadStart(this.ThreadTask));
        //makes the new thread a background thread, allowing easy termination 
        //upon closing of the application
        trd.IsBackground = true;
        //starts the thread
        trd.Start();


        /*Same process as above, but for thread2*/
        Thread trd2 = new Thread(new ThreadStart(this.ThreadTask2));
        trd2.IsBackground = true;
        trd2.Start();

        Thread trd3 = new Thread(new ThreadStart(this.ThreadTask3));
        trd3.IsBackground = true;
        trd3.Start();


        Thread trd4 = new Thread(new ThreadStart(this.ThreadTask4));
        trd4.IsBackground = true;
        trd4.Start();   

    }

    }
}

        }

Is there something I’m missing? everything else seems to do what I want

  • 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-06-08T10:21:57+00:00Added an answer on June 8, 2026 at 10:21 am

    The condition in your for loops is wrong.

    You should check for i <= 2 and not i == 2

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

Sidebar

Related Questions

My .bashrc looks something like this... export PERL5LIB=/tools/perl/Linux/${PLAT}/lib/perl5/5.10.0/${PLAT}-thread-multi export PERL5LIB=${PERL5LIB}:/tools/perl/Linux/${PLAT}/lib/perl5/5.10.0 function dev { export
I was just doing some random testing on locking in multi-threading this morning and
I want to execute an external program in each thread of a multi-threaded python
I'm really struggling with a multi-thread program that changes an IBOutlet when external variables
My program is a method to draw a circle on JPanel with multi-thread. The
I have a some computation program. Now, this program is a single-thread, I need
I have implemented a java program . This is basically a multi threaded service
I am changing a single thread program into multi thread using boost:thread library. The
I'm trying to understand how does multi-core systems work and how to program efficient
In a multi-threaded java program, what happens if a thread object T has been

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.