-2

I would like to know, how to create separate instance of a class for each thread.

For example, I have o process set of records using threads. Consider 1000 records and 5 threads. Each thread should process 200 records and write into 5 text files. First thread should write into file1, second thread into file2 and so on... How do i know which thread is currently running and how to write into the correct fie. The file processing and data processing are in one class and thread calling is in another class. If i am creating 5 threads, each thread should have it's own class. When the second thread is created, the first thread is getting disconnected. that is, only some data is written into file1 and once the second file is created its not writing the remaining data into first. Always the last file has all data and other files has only few records.

Please any help.

Thanks in advance

  • Without seeing your code it's not possible to understand what's the issue is and how to solve it. Please share your code. – Chetan Jan 21 '18 at 01:32
  • 1
    [How much research have you done](https://meta.stackoverflow.com/questions/261592) on this? – Ňɏssa Pøngjǣrdenlarp Jan 21 '18 at 01:32
  • Have you looked at [Thread local storage](https://stackoverflow.com/q/4190764) or [`ThreadStatic` v.s. `ThreadLocal`: is generic better than attribute?](https://stackoverflow.com/q/18333885) or [Thread Local Storage For C# Class Library](https://stackoverflow.com/q/7850874) or [Thread Local Storage For C# Class Library](https://stackoverflow.com/q/7850874)? Do those answer your question? – dbc Jan 21 '18 at 01:42
  • Threadstatic won't work for this condition. – user9245942 Jan 21 '18 at 14:19

1 Answers1

0

You may want to consider a different way of splitting up your work into threads. There is no correlation between a class and a thread, so there are many ways to structure your code to work in a multi-threaded way, and I would suggest an approach like the following:

// Use a queue to allow the 5 threads to pull the next record
var workItems = new ConcurrentQueue<Record>();

// Add your 1000 Records to workItems queue

// Start 5 workers to process the Records in the queue
Parallel.ForEach(Enumerable.Range(0,5), index =>
{
    // Create a separate file for each thread to work with
    var fileName = string.Format("outputFile_{0}.txt", index);

    using (var outputStream = new StreamWriter(fileName))
    {
        // Dequeue Records from the queue until they're all processed
        while (!workItems.IsEmpty)
        {
            Record record;
            if (workItems.TryDequeue(out record))
            {
                // Process each record and write to the file
                var result = ProcessRecord(record);
                outputStream.Write(result);
            }
        }
    }
})
Aaron M. Eshbach
  • 6,380
  • 12
  • 22
  • Hi Aaron, Thanks for you suggestion.You have give solution to write the output in to files using threds, But our scenario is, I have to process these 1000 records in the threads (5 max). Each records have to go for a multiple tables in the database to fetch the information.Mean to say "I want to process records using threads and once it is processed , the output should writen in to the corresponding text file immediately" – user9245942 Jan 21 '18 at 18:34
  • Yes, what I am suggesting is essentially the [Producer-Consumer Pattern](http://www.ni.com/white-paper/3023/en/), where you would have a fixed number of producer threads (say, 1) that fetch the data and add the records to be processed into a queue, and then have some number of consumer threads (say, 5) dequeue the records and process them. – Aaron M. Eshbach Jan 22 '18 at 03:16
  • I made it to work with multi threading.. currently i am facing a issue like, all the thread works maximum of 30 records. beyond 30 records, it writes randomly or leaves some records or sometimes write the same record in both files. Any idea. – user9245942 Jan 23 '18 at 00:35