Java Question How many threads are normally used?

kolbe

Active Member
Licensed User
Longtime User
For the IOIO library I'm running on a new thread (BA.submitRunnable). From time to time (the program runs non-stop for days) I get the RejectedExecutionException which I can guess is because there are more than 20 threads. Obviously I'm trying to avoid this.

If in Java I use Thread.activeCount() I typically get from 6 to 10 threads. Now the UI is one, IOIO two, logcat three, but what are these other active threads??? I do have a bluetooth connection running, GPS too, Wifi at times...

I guess my question is are these legit threads or wayward IOIO threads?
 

kolbe

Active Member
Licensed User
Longtime User
The limit of 20 threads is only related to B4A internal thread pool.
Are you using a single thread or multiple threads in your library?

The B4A library exposes the API of the IOIO library. It is possible that a call is made to the IOIO while other ones are still ongoing, due to loops and pauses. Below is an example. So to answer your question multiple threads are possible.

Ideally I would like the whole library to run on one separate thread but I don't know of an easy way to do that with 40+ methods in the API. Suggestions??

B4X:
public void pulse (final BA ba, final String EventName, final int msecHi, final int msecLow, final int Cycles)  throws ConnectionLostException
      {
         if (EventName == null) throw new RuntimeException("EventName is Null");

            Runnable r = new Runnable() {
               @Override
                 public void run() {
                  Thread.currentThread().setPriority(6);
                  Integer count=Thread.activeCount();
                  Log.i("kolbe thread count ",count.toString());
                  try {
                     for(int x = 1; x <= Cycles; x++){
                        ((DigitalOutput)getObject()).write(true);
                        android.os.SystemClock.sleep(msecHi);
                        ((DigitalOutput)getObject()).write(false);
                        android.os.SystemClock.sleep(msecLow);
                     }
                     ba.raiseEventFromDifferentThread(getObject(), null, 0, EventName + "_done", true, new Object[] {true});
                     
                  } catch (Exception e) {
                     ba.setLastException(e);
                     ba.raiseEventFromDifferentThread(getObject(), null, 0, EventName + "_done", true, new Object[] {false});
                  }
               }
            };
            BA.submitRunnable(r, this, 0);

      }
 

kolbe

Active Member
Licensed User
Longtime User
Is IOIO thread safe? Are you sure that you are allowed to interact with it from multiple threads?

The best solution is to manage your own thread and use a blocking queue to pass jobs (Runnables) to this thread. It does require some knowledge about Java threading model.

The IOIO library is thread safe.

The limit of 20 threads is only related to B4A internal thread pool.

So the error is due to attempting too many Runnables at a time? I can check Thread.activeCount() to limit this?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The IOIO library is thread safe.
Even if you can call it from different threads it will not behave as you expect it.
Consider this code:
B4X:
 ((DigitalOutput)getObject()).write(true);
                                 ((DigitalOutput)getObject()).write(true);
                                android.os.SystemClock.sleep(msecHi);
                                ((DigitalOutput)getObject()).write(false);
                                android.os.SystemClock.sleep(msecLow);
It is likely that while the current thread sleeps, another thread will write something else to the device.

So the error is due to attempting too many Runnables at a time?
Yes, runnables submitted to the internal thread pool. You can use your own thread pool or manually create threads without any limitations.

The solution is to use a single thread with some queue. See java.util.concurrent.ArrayBlockingQueue
 
Top