using System;
using System.Threading ;
using System.Threading .Tasks ;
namespace Recetas.Multithreading .R0402
{
public class PoolThreadsVsMainThread
{
public static void Main( )
{
Console.WriteLine ( "\n Aplicación consola ejecutándose sobre el ID thread: {0}" ,
Thread.CurrentThread .ManagedThreadId ) ;
var taskAsincronico = Task.Run ( ( ) =>
{
Console.WriteLine ( "ID Task {0} (asincrónico) ejecutándose sobre el ID de Thread: {1}" ,
Task.CurrentId ,
Thread.CurrentThread .ManagedThreadId ) ;
long suma = 0 ;
// Cálculo de la suma entre 1 y 1000000:
for ( int i = 1 ; i <= 1000000 ; ++ i)
{
suma += i;
}
return suma;
}
) ;
var taskSincronico = new Task< long> ( ( ) =>
{
Console.WriteLine ( "ID Task {0} (sincrónico) ejecutándose sobre el ID de Thread: {1}" ,
Task.CurrentId ,
Thread.CurrentThread .ManagedThreadId ) ;
long suma = 0 ;
// Cálculo de la suma entre 1 y 1000000:
for ( int i = 1 ; i <= 1000000 ; ++ i)
{
suma += i;
}
return suma;
}
) ;
// Inicio de ejecución Task sincrónico:
taskSincronico.RunSynchronously ( ) ;
Console.WriteLine ( ) ;
// Visualización de resultados:
Console.WriteLine ( "ID Task {0} retornó: {1:N0}" , taskSincronico.Id , taskSincronico.Result ) ;
Console.WriteLine ( "ID Task {0} retornó: {1:N0}\n " , taskAsincronico.Id , taskAsincronico.Result ) ;
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGhyZWFkaW5nOwp1c2luZyBTeXN0ZW0uVGhyZWFkaW5nLlRhc2tzOwoKbmFtZXNwYWNlIFJlY2V0YXMuTXVsdGl0aHJlYWRpbmcuUjA0MDIKewogICAgcHVibGljIGNsYXNzIFBvb2xUaHJlYWRzVnNNYWluVGhyZWFkCiAgICB7CiAgICAgICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKSAKICAgICAgICB7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgiXG5BcGxpY2FjacOzbiBjb25zb2xhIGVqZWN1dMOhbmRvc2Ugc29icmUgZWwgSUQgdGhyZWFkOiB7MH0iLCAKICAgICAgICAgICAgICAgIFRocmVhZC5DdXJyZW50VGhyZWFkLk1hbmFnZWRUaHJlYWRJZCk7CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgdmFyIHRhc2tBc2luY3JvbmljbyA9IFRhc2suUnVuICggKCkgPT4gCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJJRCBUYXNrIHswfSAoYXNpbmNyw7NuaWNvKSBlamVjdXTDoW5kb3NlIHNvYnJlIGVsIElEIGRlIFRocmVhZDogezF9IiwKICAgICAgICAgICAgICAgICAgICAgICAgVGFzay5DdXJyZW50SWQsIAogICAgICAgICAgICAgICAgICAgICAgICBUaHJlYWQuQ3VycmVudFRocmVhZC5NYW5hZ2VkVGhyZWFkSWQpOwogICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICBsb25nIHN1bWEgPSAwOwogICAgICAgICAgICAgICAgICAgIC8vIEPDoWxjdWxvIGRlIGxhIHN1bWEgZW50cmUgMSB5IDEwMDAwMDA6CiAgICAgICAgICAgICAgICAgICAgZm9yICggaW50IGkgPSAxOyBpIDw9IDEwMDAwMDA7ICsraSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHN1bWEgKz0gaTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHN1bWE7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICk7CiAgICAgICAgICAgIAogICAgICAgICAgICB2YXIgdGFza1NpbmNyb25pY28gPSBuZXcgVGFzazxsb25nPiAoICgpID0+IAogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgiSUQgVGFzayB7MH0gKHNpbmNyw7NuaWNvKSBlamVjdXTDoW5kb3NlIHNvYnJlIGVsIElEIGRlIFRocmVhZDogezF9IiwKICAgICAgICAgICAgICAgICAgICAgICAgVGFzay5DdXJyZW50SWQsIAogICAgICAgICAgICAgICAgICAgICAgICBUaHJlYWQuQ3VycmVudFRocmVhZC5NYW5hZ2VkVGhyZWFkSWQpOwogICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICBsb25nIHN1bWEgPSAwOwogICAgICAgICAgICAgICAgICAgIC8vIEPDoWxjdWxvIGRlIGxhIHN1bWEgZW50cmUgMSB5IDEwMDAwMDA6CiAgICAgICAgICAgICAgICAgICAgZm9yICggaW50IGkgPSAxOyBpIDw9IDEwMDAwMDA7ICsraSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHN1bWEgKz0gaTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHN1bWE7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICk7CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBJbmljaW8gZGUgZWplY3VjacOzbiBUYXNrIHNpbmNyw7NuaWNvOgogICAgICAgICAgICB0YXNrU2luY3Jvbmljby5SdW5TeW5jaHJvbm91c2x5KCk7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgpOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gVmlzdWFsaXphY2nDs24gZGUgcmVzdWx0YWRvczoKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJJRCBUYXNrIHswfSByZXRvcm7DszogezE6TjB9IiwgdGFza1NpbmNyb25pY28uSWQsIHRhc2tTaW5jcm9uaWNvLlJlc3VsdCk7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgiSUQgVGFzayB7MH0gcmV0b3Juw7M6IHsxOk4wfVxuIiwgdGFza0FzaW5jcm9uaWNvLklkLCB0YXNrQXNpbmNyb25pY28uUmVzdWx0KTsKICAgICAgICB9CiAgICB9Cn0=
stdout
Aplicación consola ejecutándose sobre el ID thread: 1
ID Task 1 (sincrónico) ejecutándose sobre el ID de Thread: 1
ID Task 2 (asincrónico) ejecutándose sobre el ID de Thread: 4
ID Task 1 retornó: 500,000,500,000
ID Task 2 retornó: 500,000,500,000