Your collection type should be thread-safe. It is not, this is not a problem, but you should make all calls you use thread safe, by simply wrapping each of them in the
lock
statement on the same lock object:
lock(someLockObject)
firstsortedlist.Add(item.Key, item.Value);
There is no direct relationships between number of codes and number of tasks, but if number of cores is greater or close to the number of tasks, you can effectively improve performance by adding tasks, because it improves CPU utilization level by your process, but only if they are independent and don't wait for each others; as number of tasks gets greater, you don't really improve throughput. For example, if you have just one CPU with one code, and, for the logic of the problem, it does not matter if the tasks a are executed one after another on asynchronously, parallel execution actually decrease performance, because the mechanism of parallelism itself has some overhead wasting CPU time.
You cannot apply these CPU utilization consideration for the processes where parallelism lies in the nature of the problem. For example, UI always needs threading if you have any non-circular task to execute in the UI background, because the UI thread only takes CPU time when a user sends some input and sleeps in between wasting zero CPU time. In this case, preemptive multitasking allows the process to execute some background tasks without considerable slowing down of the UI. Similar things happens if you have regular and time-consuming communication over network, hardware control, data acquisition, any time-consuming calculations, and a lot more — in all such scenarios multithreading gives you indispensable benefits even with small numbers of CPU cores, even with one.
—SA