Compartiendo para dos mundos

Hablando de programación paralela.
Tutoriales en: https://www.youtube.com/user/jambrizgdl
Twitter: @dogoteacher

Buscar este blog

lunes, 21 de diciembre de 2009

First example of threads on C#

The next code in c# generated 100 names with two arrays without threads.

public class hilo__1
{
private String[] nombres= new String[10];
private String[] apellidos= new String[10];

public hilo__1()
{
nombres[0]= "jesus";
nombres[1]= "antonio";
nombres[2]= "claudia";
nombres[3]= "Pilar";
nombres[4]= "Magdalena";
nombres[5]= "Roberto";
nombres[6]= "Elias";
nombres[7]= "Ana";
nombres[8]= "Mario";
nombres[9]= "Maria";

apellidos[0]= "perez";
apellidos[1]= "rivera";
apellidos[2]= "marte";
apellidos[3]= "lopez";
apellidos[4]= "Liz";
apellidos[5]= "Cabrera";
apellidos[6]= "Macias";
apellidos[7]= "Rodriguez";
apellidos[8]= "Mares";
apellidos[9]= "Limon";
}

public void mostrar()
{
int i, j;
for(i=0; i< 10 ; i+=1)
{
for(j=0; j<10; j+=1 )
{
System.out.println(nombres[i] + " " + apellidos[j] );
}
}
}

public static void main(String[] args)
{
hilo__1 obj= new hilo__1();
obj.mostrar();
}

}

Now, the same code but with threads in c#

using System;
using System.Threading;

namespace hijos_v2
{
public class hilo2
{
private String[] nombres= new String[10];
private String[] apellidos= new String[10];
private int inicio;
private int termino;
private String name;

public hilo2(int i, int j, String n)
{
inicio= i;
termino= j;
name= n;

nombres[0]= "jesus";
nombres[1]= "antonio";
nombres[2]= "claudia";
nombres[3]= "Pilar";
nombres[4]= "Magdalena";
nombres[5]= "Roberto";
nombres[6]= "Elias";
nombres[7]= "Ana";
nombres[8]= "Mario";
nombres[9]= "Maria";

apellidos[0]= "perez";
apellidos[1]= "rivera";
apellidos[2]= "marte";
apellidos[3]= "lopez";
apellidos[4]= "Liz";
apellidos[5]= "Cabrera";
apellidos[6]= "Macias";
apellidos[7]= "Rodriguez";
apellidos[8]= "Mares";
apellidos[9]= "Limon";
}

public void mostrar()
{
int i, j;
for(i=inicio; i< termino; i+=1)
{
for(j=0; j " + nombres[i] + " " + apellidos[j] );
}
}
}

public static void Main()
{
hilo2 obj1= new hilo2(0,5,"1");
hilo2 obj2= new hilo2(5,10,"2");
Thread h1= new Thread( obj1.mostrar );
Thread h2= new Thread( obj2.mostrar );
h1.Start();
h2.Start();
}
}
}

Each thread work with the half of names of the first array, but take all names of the second array.

No hay comentarios:

Publicar un comentario

Are you ready?