Ebin P Issac Ebin P Issac - 2 months ago 7
Java Question

passing and accepting same types, still getting incompatible types: possible lossy conversion from double to long

I have gone through other posts on the same topic, but doesn't seems to be fixing my error. In my main class I have a variable which is declared as double.

double R_b2 = 60000/r_b2;


I am passing this value to a thread

Thread prodThread2 = new Thread(new Baker(cupcakes_sharedQueue, size,R_b2), "Cupcakes");


In the constructor, I declared it as the following

private final Vector prod_sharedQueue;
private final int SIZE;
private final double rate;
public Baker(Vector prod_sharedQueue, int size, double rate) {
this.prod_sharedQueue = prod_sharedQueue;
this.SIZE = size;
this.rate = rate;


}

I am using this passed value as a timer to sleep the thread.

I am getting the error

Baker.java:36: error: incompatible types: possible lossy conversion from double to long
Thread.sleep(rate);


in the line

Thread.sleep(rate);


What is wrong? I tried to use int, it works without errors, just that I dont get the correct value I need. Other than that I tried float,double,long all are not working. TIY.

Answer

Thread.sleep is declared to take a long, not a double. You need to manually cast it:

Thread.sleep((long) rate);

Note that this will truncate down to the nearest millisecond below.