When opening a rtc-stream a thread should always grab the newest picture because otherwise there would be a huge buffer with old pictures. Also a call from getMatFromStream() should never return the same picture. If there was no new picture it should wait till a new one is available.package apclient.model;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.opencv.core.Core;import org.opencv.core.Mat;import org.opencv.videoio.VideoCapture;/** * Camera that is mounted on the model. *  * @author Marc Nussbaumer * */public class OnBoardCamera {    /**     * Contains the picture that the camera has taken.     */    private volatile Mat image;    /**     * Instance of the camera.     */    private final VideoCapture capture;    /**     * Thread that ensures that the picture is always the newest.     */    private final Thread grabberThread;    /**     * Boolean that is used to indicate if the picture is fresh.     */    private boolean activePicture = false;    /**     * Lock used for synchronization purposes.     */    private final Object lock = new Object();    /**     * Counter is used for logging and debugging.     */    private volatile int counter = 0;    /**     * Implemenation of the grabberthread.     */    private final PictureGrabber grabber;    /**     * Logger for debugging purposes.     */    static final Logger logger = LogManager.getLogger(OnBoardCamera.class);/** * Receiver for a h264 stream from a raspberry pi. Possible stream * structure: http://169.254.244.169:8080/stream/video.h264 *  * @param urlStream *            Path to the stream */public OnBoardCamera(final String urlStream) {    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);    image = new Mat();    capture = new VideoCapture();    capture.open(urlStream);    grabber = new PictureGrabber(this, lock, capture);    grabberThread = new Thread(grabber);    grabberThread.start();}/** * Reads a Mat from the open stream. The returned mat has a delay of * 200-300ms to the real time situation. *  * @return Mat from the current open Stream */public final synchronized Mat getMatFromStream() {    synchronized (lock) {        counter++;        while (!activePicture) {            try {                logger.debug(Waiting for Picture  + counter);                lock.wait();            } catch (InterruptedException e) {                logger.error(getMatFromStream error:  + e.getMessage());                e.printStackTrace();            }        }        setNewPicture(false);        // image = new Mat();        capture.retrieve(image);        return image.clone();    }}/** * Is used to inform the class that a new picture is available. *  * @param state */private final void setNewPicture(final boolean state) {    synchronized (lock) {        activePicture = state;    }}/** * Closes the reading Connection of the Stream. */public final void closeConnection() {    grabber.stopGrabber();    try {        logger.debug(connection closed);        grabberThread.join();    } catch (InterruptedException e) {        e.printStackTrace();    }    capture.release();}/** * Grabs the pictures from the stream as fast as possible otherwise they are. * stuck in the stream */private final class PictureGrabber implements Runnable {    private VideoCapture capture;    private OnBoardCamera controller;    private boolean isActive;    private Object lock;    private PictureGrabber(final OnBoardCamera controller, final Object lock, final VideoCapture capture) {        this.controller = controller;        this.capture = capture;        this.lock = lock;        isActive = true;    }    public void stopGrabber() {        isActive = false;    }    @Override    public void run() {        while (isActive) {            if (capture.isOpened()) {                synchronized (lock) {                    if (capture.grab()) {                        controller.setNewPicture(true);                        lock.notifyAll();                    }                }                try {                    Thread.sleep(20);                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }        System.err.println(Grabber stopped);    }}}Any feedback is welcome, especially about the synchronization since I just started to look deeper into that.
Read current picture with OpenCV and always return a new picture
java;opencv
private Object lock;You did it correctly in the OnBoardCamera, but you should use final in PictureGrabber as well. Even although it is a private class. Even although it is final. Even although there are no other setters for lock. It is missing and it should be added.There is a possible infinite wait:First, instantiate a new OnboardCamera. Then (whilst activePicture is false), call closeConnection. Start a new thread where you call getMatFromStream on the camera. closeConnection resolves before this newly started thread because that's how threading is likely to resolve (continue to run current thread is more likely than swapping). Next, the thread from the grabber starts. isActive is false, so the grabber stops. Next, the getMatFromStream function calling thread starts and calls getMatFromStream. It gains a lock, ups a counter and gets stuck in the loop forever....More simplified, it goes wrong whenever you call closeConnection and then try to grab two pictures. You KNOW after the first picture that the grabber has stopped, so when another picture is requested, you should signal that there will not be another picture ever.