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 {
        public native void initializeStore();
        public native void finalizeStore();
        public native synchronized int getInteger(String pKey) throws NotExistingKeyException, InvalidTypeException; ...

Get Android NDK Beginner's Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.