0% found this document useful (0 votes)
19 views7 pages

Texte Rapido Rosa

The document defines a CarRace program that simulates 3 cars in a race using threads. It creates Car objects that implement the Runnable interface and define properties like name, distance, speed. A CarRace class starts threads for each car that randomly progress in distance until finished, outputting updates. The threads are joined to wait for all cars before ending the race.

Uploaded by

Andrei Deon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Texte Rapido Rosa

The document defines a CarRace program that simulates 3 cars in a race using threads. It creates Car objects that implement the Runnable interface and define properties like name, distance, speed. A CarRace class starts threads for each car that randomly progress in distance until finished, outputting updates. The threads are joined to wait for all cars before ending the race.

Uploaded by

Andrei Deon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

import java.util.

ArrayList;
import java.util.List;
import java.util.Random;

class Car implements Runnable {


private String name;
private int distance;
private int restTime;
private int topSpeed;
private boolean finished;

public Car(String name, int distance, int restTime, int topSpeed) {


this.name = name;
this.distance = distance;
this.restTime = restTime;
this.topSpeed = topSpeed;
this.finished = false;
}

public void run() {


System.out.println(name + " started the race!");

Random rand = new Random();


int currentDistance = 0;

try {
while (!finished) {
int speed = rand.nextInt(topSpeed) + 1;
currentDistance += speed;

if (currentDistance >= distance) {


currentDistance = distance;
finished = true;
}

System.out.println(name + " is at " + currentDistance + "


meters.");

if (!finished)
Thread.sleep(1000);
}

System.out.println(name + " finished the race!");


Thread.sleep(restTime);
System.out.println(name + " is ready for the next race.");

} catch (InterruptedException e) {
System.out.println(name + " encountered an error: " + e.getMessage());
}
}
}

public class CarRace {


public static void main(String[] args) {
List<Thread> carThreads = new ArrayList<>();

Car car1 = new Car("Car 1", 1000, 2000, 80);


Car car2 = new Car("Car 2", 1000, 2500, 70);
Car car3 = new Car("Car 3", 1000, 1500, 90);
Thread carThread1 = new Thread(car1);
Thread carThread2 = new Thread(car2);
Thread carThread3 = new Thread(car3);

carThreads.add(carThread1);
carThreads.add(carThread2);
carThreads.add(carThread3);

try {
for (Thread thread : carThreads) {
thread.start();
}

for (Thread thread : carThreads) {


thread.join();
}

} catch (InterruptedException e) {
System.out.println("Race interrupted: " + e.getMessage());
}

System.out.println("Race finished!");
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Car implements Runnable {


private String name;
private int distance;
private int restTime;
private int topSpeed;
private boolean finished;

public Car(String name, int distance, int restTime, int topSpeed) {


this.name = name;
this.distance = distance;
this.restTime = restTime;
this.topSpeed = topSpeed;
this.finished = false;
}

public void run() {


System.out.println(name + " started the race!");

Random rand = new Random();


int currentDistance = 0;

try {
while (!finished) {
int speed = rand.nextInt(topSpeed) + 1;
currentDistance += speed;

if (currentDistance >= distance) {


currentDistance = distance;
finished = true;
}
System.out.println(name + " is at " + currentDistance + "
meters.");

if (!finished)
Thread.sleep(1000);
}

System.out.println(name + " finished the race!");


Thread.sleep(restTime);
System.out.println(name + " is ready for the next race.");

} catch (InterruptedException e) {
System.out.println(name + " encountered an error: " + e.getMessage());
}
}
}

public class CarRace {


public static void main(String[] args) {
List<Thread> carThreads = new ArrayList<>();

Car car1 = new Car("Car 1", 1000, 2000, 80);


Car car2 = new Car("Car 2", 1000, 2500, 70);
Car car3 = new Car("Car 3", 1000, 1500, 90);

Thread carThread1 = new Thread(car1);


Thread carThread2 = new Thread(car2);
Thread carThread3 = new Thread(car3);

carThreads.add(carThread1);
carThreads.add(carThread2);
carThreads.add(carThread3);

try {
for (Thread thread : carThreads) {
thread.start();
}

for (Thread thread : carThreads) {


thread.join();
}

} catch (InterruptedException e) {
System.out.println("Race interrupted: " + e.getMessage());
}

System.out.println("Race finished!");
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Car implements Runnable {


private String name;
private int distance;
private int restTime;
private int topSpeed;
private boolean finished;

public Car(String name, int distance, int restTime, int topSpeed) {


this.name = name;
this.distance = distance;
this.restTime = restTime;
this.topSpeed = topSpeed;
this.finished = false;
}

public void run() {


System.out.println(name + " started the race!");

Random rand = new Random();


int currentDistance = 0;

try {
while (!finished) {
int speed = rand.nextInt(topSpeed) + 1;
currentDistance += speed;

if (currentDistance >= distance) {


currentDistance = distance;
finished = true;
}

System.out.println(name + " is at " + currentDistance + "


meters.");

if (!finished)
Thread.sleep(1000);
}

System.out.println(name + " finished the race!");


Thread.sleep(restTime);
System.out.println(name + " is ready for the next race.");

} catch (InterruptedException e) {
System.out.println(name + " encountered an error: " + e.getMessage());
}
}
}

public class CarRace {


public static void main(String[] args) {
List<Thread> carThreads = new ArrayList<>();

Car car1 = new Car("Car 1", 1000, 2000, 80);


Car car2 = new Car("Car 2", 1000, 2500, 70);
Car car3 = new Car("Car 3", 1000, 1500, 90);

Thread carThread1 = new Thread(car1);


Thread carThread2 = new Thread(car2);
Thread carThread3 = new Thread(car3);

carThreads.add(carThread1);
carThreads.add(carThread2);
carThreads.add(carThread3);
try {
for (Thread thread : carThreads) {
thread.start();
}

for (Thread thread : carThreads) {


thread.join();
}

} catch (InterruptedException e) {
System.out.println("Race interrupted: " + e.getMessage());
}

System.out.println("Race finished!");
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Car implements Runnable {


private String name;
private int distance;
private int restTime;
private int topSpeed;
private boolean finished;

public Car(String name, int distance, int restTime, int topSpeed) {


this.name = name;
this.distance = distance;
this.restTime = restTime;
this.topSpeed = topSpeed;
this.finished = false;
}

public void run() {


System.out.println(name + " started the race!");

Random rand = new Random();


int currentDistance = 0;

try {
while (!finished) {
int speed = rand.nextInt(topSpeed) + 1;
currentDistance += speed;

if (currentDistance >= distance) {


currentDistance = distance;
finished = true;
}

System.out.println(name + " is at " + currentDistance + "


meters.");

if (!finished)
Thread.sleep(1000);
}

System.out.println(name + " finished the race!");


Thread.sleep(restTime);
System.out.println(name + " is ready for the next race.");

} catch (InterruptedException e) {
System.out.println(name + " encountered an error: " + e.getMessage());
}
}
}

public class CarRace {


public static void main(String[] args) {
List<Thread> carThreads = new ArrayList<>();

Car car1 = new Car("Car 1", 1000, 2000, 80);


Car car2 = new Car("Car 2", 1000, 2500, 70);
Car car3 = new Car("Car 3", 1000, 1500, 90);

Thread carThread1 = new Thread(car1);


Thread carThread2 = new Thread(car2);
Thread carThread3 = new Thread(car3);

carThreads.add(carThread1);
carThreads.add(carThread2);
carThreads.add(carThread3);

try {
for (Thread thread : carThreads) {
thread.start();
}

for (Thread thread : carThreads) {


thread.join();
}

} catch (InterruptedException e) {
System.out.println("Race interrupted: " + e.getMessage());
}

System.out.println("Race finished!");
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Car implements Runnable {


private String name;
private int distance;
private int restTime;
private int topSpeed;
private boolean finished;

public Car(String name, int distance, int restTime, int topSpeed) {


this.name = name;
this.distance = distance;
this.restTime = restTime;
this.topSpeed = topSpeed;
this.finished = false;
}
public void run() {
System.out.println(name + " started the race!");

Random rand = new Random();


int currentDistance = 0;

System.out.println("Race interrupted: " + e.getMessage());


}

System.out.println("Race finished!");
}
}

You might also like