Time for action – allocating an object with JNI
Let's define a background Watcher that will use an object shared between Java and C/C++ as a lock:
- In
Store.java
, add two new methods to start and stop a watcher thread. These methods respectively return and take along
as parameter. This value helps us hold a native pointer on the Java side:public class Store implements StoreListener { ... public native long startWatcher(); public native void stopWatcher(long pPointer); }
- Create a new file,
StoreThreadSafe.java
. TheStoreThreadSafe
class inherits from theStore
class and aims at making theStore
instances thread-safe usingsynchronized
Java blocks. Declare a static member fieldLOCK
of typeObject
and define a default constructor:package com.packtpub.store; ...
Get Android NDK Beginner's Guide - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.