Simple generic class for thread concurrency

Source: Internet
Author: User

This class is mainly used to solve the concurrency problems of some very simple linear processes:
For example, to create a loop, every time the input and output parameters P, return T.
Every time the same process is executed, but it takes a long time, so I want to use multiple threads to solve the problem. The generic type can solve the problem of uncertain parameters and return types, after the call is completed, an array of return types is returned.

Attachment: demo of simple thread concurrency

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Threading;

Namespace concurrentmanager
{
/// <Summary>
/// Parallel processing callback delegate
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "tlist"> </param>
Public Delegate void concurrentcallbackhandler <t> (list <t> tlist );

/// <Summary>
/// Handle event Delegation
/// </Summary>
/// <Typeparam name = "p"> </typeparam>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "para"> </param>
/// <Returns> </returns>
Public Delegate t concurrentexechandler <P, T> (P para );

/// <Summary>
/// Concurrent processing of generic classes, T is a generic parameter type
/// </Summary>
Public class concurrentmanager <P, T>
{
Public event concurrentcallbackhandler <t> callbackevent;

Private concurrentexechandler <P, T> executeonehandler = NULL;

/// <Summary>
/// Delegate the parameter Processing Method
// P parameter type, T is the return type
/// </Summary>
Public concurrentexechandler <P, T> executeoneevent
{
Set
{
If (executeonehandler = NULL)
Executeonehandler = value;
}
}

List <t> resultlist = new list <t> ();

/// <Summary>
/// Or the number of threads
/// </Summary>
Int atcount = 0;

Object lockflag = new object ();

Public void multiexec (P [] parameters)
{
Atcount = parameters. length;

Foreach (p msg in parameters)
{
Thread thread = new thread (New parameterizedthreadstart (handleone ));
Thread. Start (MSG );
}
}

/// <Summary>
/// Used for parallel functions
/// </Summary>
/// <Param name = "MSG"> </param>
Private void handleone (Object MSG)
{
T = executeonehandler (p) MSG );

// Processing statement
Lock (lockflag)
{
Resultlist. Add (t );
Atcount --;
}

If (atcount = 0)
{
If (callbackevent! = NULL)
Callbackevent (resultlist );
}

Thread. currentthread. Abort ();
}
}
}

I found some problems during use. I need to evaluate all public security jurisdictions in a city.
For example, there are three zones A, B, and C in city W, and the cases in each zone are evaluated (a series of queries and analyses are not described here)
The evaluation result is returned, and the evaluation results for this area are displayed in different colors on the map.
The original linear query is obviously slow. I started to use the above class for multi-threaded processing. Although the speed has been improved, new problems have emerged.
I created three threads A, B, and C to evaluate the three zones, and then triggered the callbackevent after the thread in the area with the longest time consumption was completed,
If C ends, thread C triggers callbackevent, which means C calls all delegate []. If a winform control is encountered
Crossthread (thread staggered) exception.
Therefore, I hope that after running A, B, and C, the execution of the program can return to the main thread. Therefore, the callback PROCESS OF atcount = 0 cannot be placed in the handleone method,
I moved this process to the multiexec method called by the main thread. The Code is as follows: public void multiexec (P [] parameters)
{
Atcount = parameters. length;

For (INT I = 0; I <parameters. length; I ++)
{
Thread thread = new thread (New parameterizedthreadstart (handleone ));
Thread. Start (parameters [I]);
}

// Pre-judgment to prevent excessive thread execution speed
If (atcount = 0 & callbackevent! = NULL)
{
Callbackevent (resultlist );
Return;
}

// Loop listener
While (atcount! = 0)
{
Thread. Sleep (10 );

If (atcount = 0 & callbackevent! = NULL)
{
Callbackevent (resultlist );
Break;
}
}
}

Code writing is disgusting, but the function is implemented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.