-3

I have a list List<int> Test and in order to do testing I need to make basic threads, one for filling and second for showing items from same randomly. So, the task is to fill and show randomly result in console. Of course, the problem is that my method for showing has that list as a parameter, so instance of the same might be the thing to solve. Here is my code which is just filling and then showing the items:

        static void Main(string[] args)
        {
            List<int> testList = new List<int>();
            System.Threading.Thread.Sleep(300);
            for (int y = 0; y < 50; y++)
            {
                testList.Add(y);
                Console.WriteLine(string.Format("Added item {0}!", y));
            };

            Thread testThread1 = new Thread(() => FillList(testList));
            testThread1.Start();
            Console.ReadKey();
        }

        public static void FillList(List<int> pTestList)
        {
            foreach(int x in pTestList)
            {
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine(x.ToString());
            }
        }

UPDATE:

I want to show in my console a couple of "Added item" and items. So to mix the first part and the second from the image.

enter image description here

Ferid Š. Sejdović
  • 998
  • 4
  • 18
  • 48
  • You could randomly order / shuffle the list when you're passing it (or using it, whichever you prefer): http://stackoverflow.com/questions/1287567/is-using-random-and-orderby-a-good-shuffle-algorithm – Mark Feb 12 '16 at 12:45
  • I need to use threads. That is the point. @Mark – Ferid Š. Sejdović Feb 12 '16 at 12:48
  • 2
    Sorry the question is a bit unclear on which part you're having trouble with. Do you mean: You want to fill the list in a separate thread as well and need to make sure it's done before you start the thread that randomly shows the items? (I assumed that because you weren't randomly showing anything that the question was about the random part). – Mark Feb 12 '16 at 13:01
  • @DavidPine A statement like that really needs a citation... – Austin Salonen Feb 16 '16 at 21:49
  • Dear all, can you fix your votes because my account has been banned. Thank you very much! @AustinSalonen – Ferid Š. Sejdović Mar 14 '16 at 10:56
  • Dear all, can you fix your votes because my account has been banned. Thank you very much! @Mark – Ferid Š. Sejdović Mar 14 '16 at 10:56
  • @JackDawson I haven't voted on this question so those down votes are from other people. – Mark Mar 14 '16 at 14:10

1 Answers1

1

I would use Struct or object which will have variable Position. Code then could look like:

        static void Main(string[] args)
        {
            List<int> testList = new List<int>();
            System.Threading.Thread.Sleep(300);
            Random rng = new Random(123);
            for (int y = 0; y < 50; y++)
            {
                testList.Add(y);
                Console.WriteLine(string.Format("Added item {0}!", y));
            };

            Thread testThread1 = new Thread(() => FillList(testList));
            testThread1.Start();
            Console.ReadKey();
        }

        public static void FillList(List<int> pTestList)
        {
            List<Number> testList = new List<Number>();
            Random rng = new Random(123);
            foreach (var item in pTestList)
            {
                testList.Add(new Number() { IntNumber = item, Position = rng.Next() });
            }

            foreach (Number x in testList.OrderBy(x => x.Position))
            {
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine(x.IntNumber);
            }
        }

        public struct Number
        {
            public int Position { get; set; }
            public int IntNumber { get; set; }
        }
Slasko
  • 407
  • 2
  • 8
  • Your code works fine, but that is not what I wanted to achieve.I wanted to show a bit of interweaving from first and the second thread. If it is possible of course. Word "random" was related to that. @Slasko – Ferid Š. Sejdović Feb 12 '16 at 14:09
  • you mean like split the list of numbers and write them in more threads ? – Slasko Feb 12 '16 at 14:19
  • Like split the result of first thread and the second one. I am not sure anyone understood what I wanted to ask. @Slasko – Ferid Š. Sejdović Feb 12 '16 at 14:22