0

I have a simple Computer class with 3 fields and Computers class which has Computer[] array and sortedAdd method, which adds elements to internal array in a sorted manner.

In main method I have computers array which has all records from the files I've read and results variable to which I'm trying to add all records by iterating through each Computers class object in computers array.

In main method I want to run a selected amount of threads (2 <= amount <= 7) - which will iterate through all the records I have and will add each of them to results. My Computers class needs to be thread-safe also.

I've tried this and it seems to be working, but only if I use 3 threads (which is obvious because I'm assigning one thread to one Computers object).

    public class Computers
    {
        public Computer[] _computers; //stores 'Computer' class objects.

        private readonly object _locker;
        public int _count { get; private set; } //element in array count

        public Computers(int length)
        {
            _locker = new object();
            _count = 0;
            _computers = new Computer[length];
        }

        public void sortedAdd(Computer computer)
        {
            lock (_locker)
            {
                int indexToinsert = findIndex(computer); // find a spot where element should be added
                if (indexToinsert == 0 && _count == 0 || indexToinsert == _count)
                {
                    _computers[indexToinsert] = computer;
                    _count++;
                }
                else
                {   //free up space for the element to be added.
                    for (int i = _count; i > indexToinsert; i--)
                    {
                        _computers[i] = _computers[i - 1];
                    }
                    _computers[indexToinsert] = computer;
                    _count++;
                }
                Monitor.Pulse(_locker);
            }

        }

        public int findIndex(Computer computer)
        {

            if (_count == 0)
                return 0;
            for (int i = 0; i < _count; i++)
            {
                if (computer.CompareTo(_computers[i]) <= 0)
                {
                    return i;
                }
            }
            return _count;
        }
    }

    static void Main(string[] args)
    {
        Computers[] computers = new Computers[3]; //data stored from 3 files.
        Computers results = new Computers(90); // will store all elements here.

        for (int i = 0; i < 3; i++)
            computers[i] = ReadToArray(); //each Computers object's array Computer[] is populated with 30 records.




        // --- now we take each element and add it to results.
        var threadsAdd = Enumerable.Range(0, 3).Select(i => new Thread(() =>
        {
            for (int j = 0; j < computers[i]._computers.Length; j++)
            {
                results.sortedAdd(computers[i]._computers[j]);
            }
        }));

        var threads = threadsAdd.ToList();

        foreach (var thread in threads)
        {
            thread.Start();
        }
        // wait for all threads to finish
        foreach (var thread in threads)
        {
            thread.Join();
        }
    }

How do I do the same but with.. for example 2, 4 or 5 threads instead ? Also is my Computers class thread safe?

Edit: I need to use Thread class specifically and not allowed to use thread safe collections

Blank __
  • 13
  • 4
  • I would ditch the `Thread` class and focus your attention on *Tasks*, then use *TPL Parallel* methods, and a *thread safe collection.* most of your complexity goes away – TheGeneral Oct 29 '19 at 01:59
  • @TheGeneral Well I'm asking this because I'm supposed to use Thread class and not allowed to use thread safe collections. – Blank __ Oct 29 '19 at 02:30

0 Answers0