RUNNING BACKGROUND CODE IN JAVA WITH RUNNABLE - .: .: Saiyine :. :.

Running background code in Java with Runnable

Por Saiyine Enviar correo el 2007-07-05 02:46:04 - Secciones:  JAVA ENGLISH  - Enlace permanente: 782

Within Java, there are three main ways you can execute background threads: to inherit from the Thread class, to implement the Runnable interface and the TimerTask object.

This second example shows the second way, which is implementing the Runnable interface, that is almost identical to inheriting from Thread. What's its use, then? Easy, Java doesn't support multiple inheritance, so if we only had the Thread way we would be stuck without being able of accessing others classes in this way. So here comes implementing the Runnable interface, wich allows us inheriting from wichever class we want AND running the class in the background if we want to.

The code is fairly easy, and very similar to the Thread's. See that we use implementand not extends when declaring the class.

public class ThreadSample implements Runnable
{
    public void run()
    {
        for (int i = 0; i < 5000 ; i++)
            System.out.println(i + " " + Thread.currentThread().getName());
        System.out.println("End of thread " + Thread.currentThread().getName());
    }
    public static void main (String [] args) {
        new Thread ( new ThreadSample() , "+").start();
        new Thread ( new ThreadSample() , "-").start();
        new Thread ( new ThreadSample() , ":").start();
        new Thread ( new ThreadSample() , "*").start();
    }
}

Otras entradas de la web que podrian interesarte:

SocializerMenefante Del.icio.us Digg 


Tu nombre (Nick):

Tu correo (Email):
  Necesario para tu gravatar!

Tu página (URL):

Escribe aqui tu comentario:

       :noworry: :roll: :huh: :push: :OO 8) 8O :( :) :? :D :P :o :x :| ;) ^^ xD



Vista previa activada.

Vista previa (6/6/6, 6:66)


Fondos de pantalla

. . .

Recomendamos

  • Betsson bahis adresi betsson iddaa bahis
  • Canli bahis betsson bahis sitesinde.

. . .

Descargas

  • ApagaPC
    apagapc241.exe  (3489)
  • LimpiaDocus
    LimpiaDocus001.exe  (2767)
  • RCM
    rcm001.zip  (2346)
  • Popmail
    popmail-0.4-psmn.tar.gz  (2410)
  • Manual de Delphi en PDF
    delphi_pdf.zip  (3009)
  • Evangelio del Perl
    Evangelio_del_perl.pdf  (2822)
  • Excel Simpsons
    Excel Simpsons  (3008)
  • . . .

    Proyectos Online

  • Saiyine Store
  • Kunowalls!!!
  • Fondos de pantalla
  • Picaday: imagenes sexys o chocantes.
  • Scarlett: fotos de Scarlett Johansson
  • WhatsmyIP: obtener tu IP pública
  • Uma Thurman: galeria de Uma Thurman
  • FunPics: imagenes graciosas
  • . . .

    Blogs

  • Por lo que más querais, no entreis a estos: Tapanez, Yhandros, Onez.

  • Otros en español: El Mundo Today, La libreta de Van Gaal, Chavalina, Kirai, Mundo Geek, Microsiervos.

  • Mis lecturas en guiri: Michael Yon, Coding Horror, YCombinator news, MySQL Performance, Slashdot.

    . . .
  • Utilidades

  •  Coral  (1196), cacheando webs
  •  Spam.la  (1431), correo de usar y tirar
  •  Mailinator  (1356), correo de usar y tirar
  •  Bug me not  (1219), absurdos registros di NO
  •  Trashmail  (1251), correo de usar y tirar

    . . .

  • Mis wikis

  • Gravastar, VirtualDub, Samuel Eilenberg

    Se suspenden las traducciones de la wikipedia hasta que no se aclaren las condiciones de su adquisición práctica por parte de google.

    . . .

  • Página web ©2001-2010 Saiyine generada en s, con 4639631 visitas en total, hoy (de un total de 0 previstas).

    . . .