Van icon

JAVA THREADS

Links


    // THREADS O HILOS
    Thread / Runnable

    Class Thread {}
    Thread.sleep(4)

    // Crear hilos:
    //Clase con interfaz Runnable, write code in run method, instanciate class y almacenar instancia en variable de tipo Runnable
    //crear instancia de la clase Thread
    //Poner en marcha hilo de ejecución


    //1- Clase con interfaz Runnable
    
    class PelotaHilos implements Runnable{
        public PelotaHilos(Peloga unaPelota, Component unComponente){
            pelota = unaPelota;
            componente = unComponente;
        }

        public void run(){
            for(int i = 0; i <=1000; i++){
                system.out.print(i);

                try{Thread.sleep(5)}
                catch(InterruptedException e){e.printStackTrace();}
            }
        }
        private Pelota pelota;
        private Component componente;
    }

    Runnable r = new PelotaHilos(pelota, lamina);
    Thread t = newThread(r);
    t.start();



    //