Informática, perguntado por sdmota, 10 meses atrás

Diferente de implementações que utilizam a classe Thread diretamente, as que utilizam a interface Runnable possuem algumas particularidades. Após instanciar o objeto que possui a interface Runnable, é necessário instanciar também um objeto da classe Thread que receba em seu método construtor uma referência ao objeto da interface Runnable, bem como um nome para a thread criada. Após instanciar este objeto da classe Thread, pode-se iniciar a thread a partir do seu método start().

Qual exemplo de uma classe que implementa a interface Runnable, bem como da chamada desta classe a partir da criação de um objeto de Thread está correta:

Escolha uma:

a. public class ExemploRunnable implements Runnable {
public void run() {
int total = 0;
while ((int)(Math.random()*100) != 50)
total++;
}
}
public class Main {
public static void main(String[] args) {
ExemploRunnable r = new ExemploRunnable();
Thread t = new Thread(r, "Thread 1");
t.start();
}
}

b. public class ExemploRunnable implements start{
public void run() {
int total = 0;
while ((int)(Math.random()*100) != 50)
total++;
}
}
public class start {
public static void main(String[] args) {
ExemploRunnable r = new ExemploRunnable();
Thread t = new Thread(r, "Thread 1");
t.start();
}
}

c. public class ExemploRunnable implements Runnable {
public void start () {
int total = 0;
while ((int)(Star.random()*100) != 50)
total++;
}
}
public class star {
public static void main(String[] args) {
ExemploRunnable r = new ExemploRunnable();
Thread t = new Thread(r, "Thread 1");
t.start();
}
}

d. public class implements start {
public void run() {
int total = 0;
while ((int)(Math.random()*100) != 50)
total++;
}
}
public class Main {
public static void main(String[] args) {
ExemploRunnable r = new ExemploRunnable();
Thread t = new Thread(r, "Thread 1");
t.start();
}
}

e. public class ExemploRunnable implements {
public void run() {
int total = 0;
while ((int)(Math.random()*100) != 50)
total++;
}
}
public class Start {
public static void main(String[] args) {
ExemploRunnable r = new ExemploRunnable();
Thread t = new Thread(r, "Thread 1");
t.start();
}
}

Soluções para a tarefa

Respondido por Jonas1578
16

public class ExemploRunnable implements Runnable {

public void run() {

int total = 0;

while ((int)(Math.random()*100) != 50)

total++;

}

}

public class Main {

public static void main(String[] args) {

ExemploRunnable r = new ExemploRunnable();

Thread t = new Thread(r, "Thread 1");

t.start();

}

}


gabrielcosmai: e.
public class ExemploRunnable implements Runnable {

public void start () {

int total = 0;

while ((int)(Star.random()*100) != 50)

total++;

}

}

public class star {

public static void main(String[] args) {

ExemploRunnable r = new ExemploRunnable();

Thread t = new Thread(r, "Thread 1");

t.start();

}

}
Respondido por Xavier80
10

A RESPOSTA CORRETA É

public class ExemploRunnable implements Runnable {

public void run() {

int total = 0;

while ((int)(Math.random()*100) != 50)

total++;

}

}

public class Main {

public static void main(String[] args) {

ExemploRunnable r = new ExemploRunnable();

Thread t = new Thread(r, "Thread 1");

t.start();

}

}

Perguntas interessantes