Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!
public class Tarefa implements Runnable { private final String nome; public Tarefa(String nome) { this.nome = nome; } @Override public void run() { for (int i = 0; i <= 100; i = i + 20) { System.out.println("Tarefa " + nome + ": " + i + "%"); } } }
Thread t1 = new Thread(new Tarefa("A")); Thread t2 = new Thread(new Tarefa("B")); t1.start(); t2.start();
Thread t1 = new Thread(new Tarefa("A")); Thread t2 = new Thread(new Tarefa("B")); t1.start(); Thread.sleep(1); t2.start();
Thread t1 = new Thread(new Tarefa("A")); Thread t2 = new Thread(new Tarefa("B")); t1.start(); t2.start(); System.out.println("Fim da thread principal.");
Thread t1 = new Thread(new Tarefa("A")); Thread t2 = new Thread(new Tarefa("B")); t1.start(); t2.start(); System.out.println("Fim da thread principal.");
Thread t1 = new Thread(new Tarefa("A")); Thread t2 = new Thread(new Tarefa("B")); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("Fim da thread principal.");
@Override public void run() { while (continua()) { int proximoNumero = proximo(); if (!numeros.add(proximoNumero)) { System.out.println("Colisão: " + proximoNumero); } } } }
Runnable contador = new Contador(); for (int i = 0; i < 10; i++) { Thread t = new Thread(contador); t.start(); }