Đức Bùi
Đức Bùi

Reputation: 527

Handle Queue faster

I have a queue , it receives data overtime. I used multi thread to dequeue and save to database. I create an Array of Thread to do this job.

for (int i = 0; i < thr.Length; i++)
{
    thr[i] = new Thread(new ThreadStart(SaveData));
    thr[i].Start();
}

SaveData

Note : eQ and eiQ is 2 global queue. I used while to keep thread alive.

public void SaveData()
{

    var imgDAO = new imageDAO ();
    string exception = "";
    try
    {
        while (eQ.Count > 0 && eiQ.Count > 0)
        {
            var newRecord = eQ.Dequeue();
            var newRecordImage = eiQ.Dequeue();                        

            imageDAO.SaveEvent(newEvent, newEventImage);

            var storepath = Properties.Settings.Default.StorePath;
            save.WriteFile(storepath, newEvent, newEventImage);
        }
    }
    catch (Exception e)
    {                
        Global._logger.Info(e.Message + e.Source);
    }
}

It did create multi thread but when I debug, only 1 thread alive, the rest is dead. I dont know why ? Any one have idea? Tks

Upvotes: 0

Views: 305

Answers (2)

Christoffer
Christoffer

Reputation: 12910

As mentioned in the comments, your threads will only live as long as there are elements in the queues, as soons as both are emptied the threads will terminate. This could explain why you see only one living thread while debugging.

A potential answer to you question would be to use a BlockingCollection from the System.Collections.Concurrent classes instead of a Queue. That has the capability of doing a blocking dequeue, which will stop the thread(s) until more elements are available for processing.

Another problem is the nice race condition between eQ and eiQ -- consider using a single queue with a Tuple or custom data type so you can dequeue both newRecord and newRecordImage in a single operation.

Upvotes: 1

Kamil
Kamil

Reputation: 13931

You are using WriteFile in that thread function.

Is this possible, that you trying to write file that may be locked by another thread (same filename or something)?

And one more thing - saving data on disk by multiple threads - i dont like it.

I think you should create some buffer instead of many threads and write it every few records/entries.

Upvotes: 1

Related Questions