0
I would like to create a program to act multiple threads in a process, making it faster, that is, the threads working together to finish the service faster. But each one works individually in the process.
Sample code:
class Program
{
static void Main(string[] args)
{
Soma s = new Soma();
Thread[] Threads = new Thread[5];
for (int i = 0; i < 5; i++)
{
Threads[i] = new Thread(new ThreadStart(s.PrintNumbers));
}
foreach (Thread t in Threads)
t.Start();
Console.ReadLine();
}
}
class Soma
{
double soma = 0;
public void PrintNumbers()
{
for (int i = 0; i < 5; i++)
{
if (i%2 != 0)
{
soma -= i;
}
else
{
soma += i;
}
}
Console.WriteLine(soma);
}
}
This code is very weird, probably has errors, but I can not say because I do not know the goal. The ideal is to use
Task
and notThread
and in this case run slower than done sequentially. https://answall.com/q/123173/101 and https://answall.com/q/1946/101– Maniero
So the goal is to really respond if a multi-threaded application will do better, but I’m not getting multiple threads to work in the same process. (educational purposes)
– Eduardo
Not to say only with an isolated case like this, for this your code, only the fact that the new thread allocates new features would slow down.
– Intruso
the code itself is not that, it’s just an example
– Eduardo