Hilos Java; Ejemplo 2

Implementando la interfaz Runnable:

class ThreadConRunnable implements Runnable {
   String palabra;

public ThreadConRunnable (String _palabra) {
   palabra = _palabra;
}

public void run() {
   for (int i = 0; i<100; i++) {
      System.out.print (palabra);
   }
}

public static void main (String args[]) {
   ThreadConRunnable a = new ThreadConRunnable ("[A] ");
   ThreadConRunnable b = new ThreadConRunnable ("+B+ ");
   ThreadConRunnable c = new ThreadConRunnable ("-C- ");
   ThreadConRunnable d = new ThreadConRunnable ("^D^ ");
   Thread t1 = new Thread (a);
   Thread t2 = new Thread (b);
   Thread t3 = new Thread (c);
   Thread t4 = new Thread (d);
   t1.start();
   t2.start();
   t3.start();
   t4.start();
   System.out.println ("<>");
 }
}


Última actualización: 10 de marzo de 2004
abia@dlsi.ua.es