I ran into a problem, I hope someone here can help me. Right now, I have used too many while (true) loops in my program, so it works a bit slowly.
I'm not sure if I am doing something wrong with my code, or if this is the only solution.
Have a class that receives data (doubles) from another class. Want to run a while loop, which should only run when the data is not equal to 0. I have some code here:
while (true) {
while (kWh != 0) {
try {
queue.put(kWh);
} catch (InterruptedException e) {
System.out.println("Could not put data into queue");
}
kWh = 0;
}
}
@Override
public void SendPower(double power_data) throws RemoteException {
ReceivePowerData.kWh = power_data;
}
, SendPower . 0, , while . , while (true), , , .
? ?
wait(); , , , , .. , notify(); , .
:
synchronized (this) {
System.out.println("Wait for response from sensor");
try {
wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("Continue");
while (kWh != 0) {
try {
queue.put(kWh);
} catch (InterruptedException e) {
System.out.println("Could not put data into queue");
}
kWh = 0;
}
}
, , .
@Override
public void SendPower(double power_data) throws RemoteException {
ReceivePowerData.kWh = power_data;
synchronized (this) {
notify();
}
}
, SendPower , , , . am close?