0% found this document useful (0 votes)
65 views10 pages

Curso de Java: Threads: Interface Runnable

The document discusses two approaches to creating threads in Java - extending the Thread class and implementing the Runnable interface. When extending Thread, only the run method needs to be overridden. When implementing Runnable, the run method also needs to be implemented. The Runnable approach allows a class to extend another class in addition to implementing Runnable. If no other Thread methods need to be overridden, the Runnable approach may be better.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views10 pages

Curso de Java: Threads: Interface Runnable

The document discusses two approaches to creating threads in Java - extending the Thread class and implementing the Runnable interface. When extending Thread, only the run method needs to be overridden. When implementing Runnable, the run method also needs to be implemented. The Runnable approach allows a class to extend another class in addition to implementing Runnable. If no other Thread methods need to be overridden, the Runnable approach may be better.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Curso de Java

68
Threads:
interface
Runnable
public class MinhaThread extends Thread {

private String nome;


private int valorAdormecida;

public MinhaThread(String nome, int valorAdormecida){


this.nome = nome;
this.valorAdormecida = valorAdormecida;
start();
}

public void run(){


System.out.println(nome + "foi iniciada");

try {
for (int i=0; i<6; i++){
System.out.println(nome + " tem contador valor " + i);
Thread.sleep(valorAdormecida);
}
} catch (InterruptedException e) {
System.out.println(nome + " foi interrompida");
}

System.out.println(nome + "foi terminada");


}

public static void main(String[] args){


MinhaThread thread = new MinhaThread("Thread1", 500);
MinhaThread thread2 = new MinhaThread("Thread2", 900);
}
}
public class MinhaThreadRunnable implements Runnable {

private String nome;


private int valorAdormecida;

public MinhaThreadRunnable(String nome, int valorAdormecida){


this.nome = nome;
this.valorAdormecida = valorAdormecida;
//start(); Não precisa chamar o método start
}

public void run(){


Também é preciso implementar
System.out.println(nome + "foi iniciada");
o método run
try {
for (int i=0; i<6; i++){
System.out.println(nome + " tem contador valor " + i);
Thread.sleep(valorAdormecida);
}
} catch (InterruptedException e) {
System.out.println(nome + " foi interrompida");
}

System.out.println(nome + "foi terminada");


}

public static void main(String[] args){


MinhaThread thread = new MinhaThread("Thread1", 500);
MinhaThread thread2 = new MinhaThread("Thread2", 900);
}
}
public class MinhaThreadRunnable implements Runnable {

private String nome;


private int valorAdormecida;

public MinhaThreadRunnable(String nome, int valorAdormecida){


this.nome = nome;
this.valorAdormecida = valorAdormecida;
//start();
}

public void run(){


Também é preciso implementar
System.out.println(nome + "foi iniciada");
o método run
try {
for (int i=0; i<6; i++){
System.out.println(nome + " tem contador valor " + i);
Thread.sleep(valorAdormecida);
}
} catch (InterruptedException e) {
System.out.println(nome + " foi interrompida");
}

System.out.println(nome + "foi terminada");


}

public static void main(String[] args){


MinhaThread thread = new MinhaThread("Thread1", 500);
MinhaThread thread2 = new MinhaThread("Thread2", 900);
}
}
public static void main(String[] args){

MinhaThreadRunnable thread = new MinhaThreadRunnable("Thread1", 500);


MinhaThreadRunnable thread2 = new MinhaThreadRunnable("Thread2", 900);

Thread t1 = new Thread(thread); Só que precisamos criar uma thread e passar a


Thread t2 = new Thread(thread2); classe Runnable criada

t1.start();
t2.start();
}
public MinhaThreadRunnable(String nome, int valorAdormecida){
this.nome = nome;
this.valorAdormecida = valorAdormecida;
new Thread(this).start(); Ou podemos fazer isso diretamente do construtor
}
Qual abordagem é melhor?

• Quando fazemos extends da Thread, o único método que


precisa ser sobreposto é o run
Qual abordagem é melhor?

• Quando fazemos extends da Thread, o único método que


precisa ser sobreposto é o run

• Quando implementamos Runnable, também precisamos


implementar o método run
Qual abordagem é melhor?

• Quando fazemos extends da Thread, o único método que


precisa ser sobreposto é o run

• Quando implementamos Runnable, também precisamos


implementar o método run

• Com a classe Runnable, podemos extender qualquer


outra classe
Qual abordagem é melhor?

• Quando fazemos extends da Thread, o único método que


precisa ser sobreposto é o run

• Quando implementamos Runnable, também precisamos


implementar o método run

• Com a classe Runnable, podemos extender qualquer


outra classe

• Se não for sobrepor qualquer outro método da classe


Thread, pode ser melhor usar a classe Runnable

You might also like