Listening to data, is (right) the right solution? Java

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?

+4
3

. Java .

Java

http://en.wikipedia.org/wiki/Observer_pattern http://www.tutorialspoint.com/design_pattern/observer_pattern.htm

.

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();
    }
}
+5

.

, synchronized, .

private final Queue<Integer> queue = ArrayDeque<Integer>();

public synchronized void addKWh(int kwh){
   queue.put(kWh);
   notifyAll(); //tell other thread queue has something
}

//running on other thread
public synchronized void efficientQueueProcessor() { 
    while (true) { //really should have an exit condition
        Integer kwh = null;
        synchronized { //mainly because wait() must be in sync, but also for queue
            Integer kwh = queue.poll();
            if (kwh == null) {
              //queue is empty
              wait(); //thread will wait here efficiently until something is added
              continue; //so null is not processed
            }
        }
        processFigure(kwh); // out of sync block
    }
}

public void processFigure(int kwh){
    // do work with kwh
}

, :

@Override
public void SendPower(double power_data) throws RemoteException {
    ReceivePowerData.addKWh(power_data);
}
0
source

All Articles