Ebin P Issac Ebin P Issac - 1 year ago 78
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

in the line


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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download