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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:43:56+00:00 2026-05-15T13:43:56+00:00

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ThreadDemo { class

  • 0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadDemo
{
    class Program
    {
        static public  List<int> temp = new List<int >();
        static  public  List<Thread> worker = new List<Thread>();
        static public List<List<int>> Temporary = new List<List<int>>();
        static void Main(string[] args)
        {
            temp.add(20);
            temp.add(10);
            temp.add(5);
            foreach (int k in temp)
            {
                int z = 0;
                worker[z] = new Thread(() => { sample(k); });
                worker[z].Name = "Worker" + z.ToString();
                worker[z].Start();
                z++;
            }
        }
        public static void sample(int n)
        {
            List<int> local = new List<int>();
            for (int i = 0; i < n; i++)
            {
                local.Add(i);
            }
            Temporary.Add(local);
        }
    }
}

in this program i have the problem in thread when start the foreach loop in main program creates the three thread and also starts that thread.In that first thread operation is longer than other so it will take some time but other thread completed before the first
due to this order in temporary is changed .i need temporary list order as same as temp list order.how can achieve this using thread

  • 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-15T13:43:56+00:00Added an answer on May 15, 2026 at 1:43 pm

    Here is a quick stab at your code:

    class Program
    {
        static public List<int> temp = new List<int >();
        static public List<Thread> worker = new List<Thread>();
        static public List<List<int>> temporary = new List<List<int>>();
        static public object sync = new object();
    
        static void Main(string[] args)
        {
            temp.add(20);
            temp.add(10);
            temp.add(5);
    
            // Add a corresponding number of lists
            for( int i = 0; i < temp.Count; ++i)
            {
                temporary.Add(new List<int>);
            }
    
            // As Jon Skeet mentioned, z must be declared outside the for loop
            int z = 0;
            foreach (int k in temp)
            {
                // As Jon Skeet mentioned, you need to capture the value of k
                int copy = k;
    
                Thread t = new Thread(() => { Sample(copy, z); });
                t.Name = "Worker" + z.ToString();
    
                // set the thread to background, so your thread is 
                // properly closed when your application closes.
                t.IsBackground = true; 
                t.Start();
    
                // Calling worker[z] will always going to be out of bounds
                // because you didn't add anything to to the worker list,
                // therefore you just need to add the thread to the worker
                // list. Note that you're not doing anything with the worker
                // list, so you might as well not have it at all.
                worker.Add(t);
                z++;
            }
        }
    
        // Supply the order of your array
        public static void Sample(int n, int order)
        { 
            for (int i = 0; i < n; i++)
            {
                // Technically in this particular case you don't need to 
                // synchronize, but it doesn't hurt to know how to do it.
                lock(sync)
                {
                    temporary[order].Add(i);
                }
            }
    }
    

    Now the temporary list should contain the other lists in the correct order (same as your tmp order). Your title does mention scheduling, but I’m not sure why you need scheduling here or what exactly you’re trying to learn about scheduling.

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

Sidebar

Related Questions

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication1 { class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace LearnThread { class
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace EfTestFactory { public abstract class
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Text; namespace secondMvc.MyControls
This is my problem: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1
I have the following code using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace
This is my code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;
using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Text.RegularExpressions; using System.Threading;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
This C#/WPF code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using

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.