C ++ / Java Links: Which Side Should Multithreading Be Implemented?

I am implementing a C ++ class called HttpDataStream

in my library. I need to make JNI bindings to use my Android library, with Objective-C bindings for iOS.

I need to create a dedicated class HttpDataStream

in Java and Objective-C, but I'm wondering if these classes actually block the entire application until the download is complete.

For example, call a method read

from a class HttpDataStream

on the Android side:

std::streamsize DataStreamJava::read(double value) {
  jmethodID m = jni->GetMethodID(j_dataStream_class_,
                                 "read", "(D)J");
  jni->CallLongMethod(j_dataStream_global_, m);
  return 0;
}

      

Let's say the method read

will download a file and return the number of bytes downloaded. I currently think that even if I use Threads and Runnables on the Android side, the C ++ library actually blocks until the download is complete.

So I ask myself the following questions:

  • Should multithreading be implemented on the C ++ and Java side?
  • Should I implement a DataStreamObserver in C ++ that will be called after the download is complete?
+3


source to share


2 answers


The answer depends on what you expect from multithreading:

a) pipelining related activities from one side

If your read()

Java side is doing multiple dependent things like reading, interpreting, or preprocessing data and ultimately rendering it, you might want to consider multithreading on the java side.

But still, you do it from this side, your only call to the stream class' read()

will only return when all mutlithreaded actions on the java side are complete. So yes, in this case the C ++ code is stuck waiting for a return.

b) do something unrelated

So, if you want your C ++ code to do something else when reading data, but is not tied to acquired data, you should consider multithreading on the C ++ side (remember to attach the JNI framework to new topics!) ...



c) conveyor related actions on both sides

But if you need to process some of the acquired Java data with one side of C ++ (for example, starting to display the data), you can choose one of the following two ways:

  • multithreading in C ++ if you have a way to concurrently access data that is read in java (like reading small chunks or a java function providing access to fillable buffers)
  • multithreading in java calling native C ++ functions like a callback every time some partial data is ready: in this case JNI will take care of running each C ++ callback function in a separate thread corresponding to the Java thread.

Browser in java or C ++

It depends on your choice and the restrictions associated with previous elections. Without knowing more, it is difficult to give you objective and useful advice.

+1


source


If this is a longer download operation, then under Android it is recommended to use it from a thread inside (possibly foreground) of the service. This way the system will not kill your application as soon as the user hides it. In this scenerio, it is better to create a java side stream.

In the application I am working on, I have a lot of my own code that runs in my own threads. This code performs HTTP communication by calling methods of the java class, which wraps various HTTPClient methods for Android. These are mostly small amounts of data.

So:

Should multithreading be implemented on both the C ++ and Java side?



for small amounts of data I would do streams in C ++ code and call a java class that wraps an HTTPClient or URL. You can use pthreads for these which should work on android and ios with minor modifications.

For large amounts of data, at least under android - use a java stream in the service. This actually makes code operations difficult, such as reading in C ++.

I'm not sure about streaming data, which I understand, this is your case if you need data immediately - then your own streams must be created.

+1


source







All Articles