I have faced a problem i hope someone here can help me with. Right now, I have used way too many while(true) loops in my program, so it runs a little slow.
I'm not sure if i am doing something wrong with my code, or if this is the only solution.
Have a class which receive data (doubles) from another class. Want to start a while loop which only should run when the data is not 0. Some code i have 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;
}
As you can see, when the SendPower method runs, the kWh variable gets updated. When this variable is not 0, i want the while loop to run and insert the value to the queue. My problem is that i am using while(true) so it keeps running that loop, even when no data are received, and that is a waste.
What can i else do? any idea?
Update
I have tried to use the wait(); method, and that seem to work, it waits.. but i don't know how i should implement the notify(); method which should start it again.
code:
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;
}
}
Then i tried to code it in the method, but i really think this is wrong..
@Override
public void SendPower(double power_data) throws RemoteException {
ReceivePowerData.kWh = power_data;
synchronized (this) {
notify();
}
}
So when the SendPower method are called from another program, it should run, and then it should start, but this does not work.. am i close?
It's always true, it's never false. Some people use while(true) loops and then use break to exit them when a certain condition is true, but it's generally quite sloppy practice and not recommended. Without the use of break , return , System.
While loops are programming structures used to repeat a sequence of statements while a condition is True . They stop when the condition evaluates to False . When you write a while loop, you need to make the necessary updates in your code to make sure that the loop will eventually stop.
While loop is used to execute a block of code repeatedly until given boolean condition evaluated to False. If we write while True then the loop will run forever.
while (true) is not inherently bad practice. Certain uses of while (true) are bad practice, mostly busy-waits where other kinds of waits would be more appropriate, but there's nothing wrong with while (true) itself.
You can use the observer pattern. Java has standard interfaces to implement it.
There are loads of tutorials on this pattern in Java
http://en.wikipedia.org/wiki/Observer_pattern http://www.tutorialspoint.com/design_pattern/observer_pattern.htm
This is a very simplistic example.
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
public class ObserverPattern {
private static class PowerObserverable extends Observable implements Runnable {
public void run() {
Random random = new Random();
for (int x = 0; x < 10; x++) {
double kwh = random.nextDouble() * 4;
setChanged();
notifyObservers(kwh);
}
}
}
// Make sure this class is thread safe
private static class PowerObserver implements Observer {
@Override
public void update(Observable o, Object kwh) {
System.out.println(kwh);
}
}
public static void main(String[] args) {
PowerObserverable observerable = new PowerObserverable();
observerable.addObserver(new PowerObserver());
new Thread(observerable).start();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With