O'Reilly logo

Android NDK Beginner's Guide by Sylvain Ratabouil

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – running a background thread

Let's add some synchronization capabilities on the Java first:

  1. Open Store.java created in the previous chapter. Create two new native methods, initializeStore() and finalizeStore(), to start/stop the watcher thread and initialize/destroy the store when activity is started and stopped, respectively.

    Make every Store class's getter and setter synchronized, as they are not allowed to access and modify store entries while the watcher thread iterates through them:

    public class Store {
        static {
            System.loadLibrary("store");
        }
    
        public native void initializeStore();
        public native void finalizeStore();
    
        public native synchronized int getInteger(String pKey) throws NotExistingKeyException, InvalidTypeException; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required