Home > Back-end >  This requirement, how to multithreading implementation is quite reasonable, dynamically create threa
This requirement, how to multithreading implementation is quite reasonable, dynamically create threa

Time:10-04

/
{
"Type" : "type001,"
"MSG" : {
"Status" : true,
"Location", "Shanghai", "
}
},
{
"Type" : "type001,"
"MSG" : {
"Status" : true,
"Location" : "Beijing", "
}
},
{
"Type" : "type002,"
"MSG" : {
"Status" : false,
"Location" : "through"
}
},
{
"Type" : "type003,"
"MSG" : {
"Status" : true,
"Location" : "Hong Kong", "
}
}
]



Such as an interface that will receive the list data to come over, and then the corresponding processing, want to use multiple threads processing, specific as follows:

The same type of data in a separate thread processing,
Like the first time a type001 and type002 two kinds of data type, then create two threads to separate the two type of data,
Second type001, type002 and type03, then type001, type002 first create the corresponding thread processing, type03 before will also create a thread to handle alone,
Back again as long as the new type can create a new thread to handle this type of data,
Each type, type of data in short has a separate thread processing, each thread only deal with the all the data type, the type has a cap would not have been increased number

This is how to create a thread, or how to design to realize more appropriate, best code
Thank you for your warrior directions

CodePudding user response:

Which master has train of thought

CodePudding user response:

Create a dictionary, the key is the type, the value is the corresponding data array (can consider to use queue, first in first out),
Then different thread to monitor these array processing,

CodePudding user response:

refer to the second floor water 2 reply:
create a dictionary, the key is the type, the value is the corresponding data array (can consider to use queue, first in first out),
Then different thread to monitor these array processing,


These data is taken from the queue, but one may take several different types, so I want to a special type of data separate thread processing, would like to know how to create the better now

CodePudding user response:

Ah, take the data from a unified big queue,
According to different type, distributed to different small queue to ah,

CodePudding user response:

You this design has a drawback, when at the same time have a request for many times, is to use the same thread, can cause obstruction
As change of demand, do not need to be sure to use the same thread, the same type as long as the thread is idle, then use the thread pool is the best,
And, of course, if you will use thread local storage, or in the process of using a thread will pollution to the thread state, a thread can only be in the service of a type, then the other
  • Related