I have Car object which implements Runnable
as below
Car implements Runnable{
int id;
public Car(int id) {
id = this.id;
}
public boolean getIsAvailable() {
return isAvailable;
}
}
class CabService{
static List<Thread> cars = new ArrayList<Thread>();
public static void main(String[] args) {
for(int i=0; i< 3; i++)
{
cars.add(new Thread(new Car(i)));
}
}
}
In CarService class I have list of cars as below and trying to iterate over list and cast the object to Car as below
List<Car> availableCars = new ArrayList<Car>();
List<Thread> cars = CabService.cars;
for(Thread carThread : cars) {
Car car = (Car)carThread;
if(car.getIsAvailable())
availableCars.add(car);
}
return availableCars;
When I try to cast the carThread
to car
I get compilation error Cannot cast from Thread to Car
Can someone help me how to fix this?