Skip to content

Latest commit

 

History

History
55 lines (48 loc) · 1011 Bytes

Exercici pràctica Thread.md

File metadata and controls

55 lines (48 loc) · 1011 Bytes

Fer un programa que simula una cursa de 100 metres llisos de 2 corredors Sortida del programa ha de ser per exemple:

  • Corredor 1 ha recorregut 1m
  • Corredor 1 ha recorregut 2m
  • Corredor 2 ha recorregut 1m
  • ...
  • Corredor 2 ha recorregut 100m
  • Corredor 1 ha recorregut 100m
public abstract class Thread {
	public void jun() {
		...
	}
	public void run() {}
	
	public void start() {
		...
	}

	public Thread(Runnable r) {
		...
	}
}

public interface Runnable {
	public void run();
}

start() crea un nou fil d'execució i dins d'aquest fil hi executa el run.

class Corredor extends Persona implements Runnble {
	public Corredor (String nom) {
		super(nom)
	}
}
public class Cursa {
	public static void main(String[] args) {
		Corredor c1 = new Corredor("Corredor 1");
		Corredor c2 = new Corredor("Corredor 2");

		Thread t1 = new Thread(c1);
		Thread t1 = new Thread(c2);
		t1.start();
		t2.start();
		t1.join();
		t2.join();
		System.out.println("Cursa acabada");
	}
}