new Thread() Using the existing thread

Skip to first unread message

Ralph Peters

Jun 30, 2021, 4:53:17 PMJun 30
to j2objc-discuss
I'm creating a new thread for some background processing. On Android this works fine, but on iOS it appears to run the code in the original thread not a new one. 

thread = new Thread() {
  public void run() {
    for (;;)
    Message msg = getNextMsg();
    // more code here

Screenshot below, showing breakpoints on the new Thread() statement, which is the original thread:


and the msg assignment, which appears to be the same thread as the original: 


I'm definitely calling thread.start() not

Any ideas what I might be doing wrongly?

XCode: v12.5.1
J2ObjC: v2.5

Tom Ball

Jun 30, 2021, 8:05:14 PMJun 30
I'm not sure why the debugger is showing that, but creating new threads definitely works (it would be impossible to support things like Java concurrency without them).

Threads are created with unique ids, which can be queried with Thread.getId(). They can also be created with names, so you can name your new thread something like "MessageThread" (if no name is given, they are named "Thread-" plus the thread id). So I suggest printing out each thread's name and id to verify they're different. Here's a simple example:

class HelloThreads {
  static void logThreadName() {
    Thread t = Thread.currentThread();
    System.out.println("thread name: " + t.getName() + " id: " + t.getId());

  public static void main(String... args) throws Exception {
    // Log the main thread.

    // Start a new thread and log it.
    new Thread(new Runnable() {
      public void run() {
    }, "MessageThread").start();

  // Give MessageThread's logThreadName() time to run before exiting.

To run on MacOS:

$ j2objc
$ j2objcc HelloThreads.m
$ ./a.out HelloThreads
thread name: main id: 1
thread name: MessageThread id: 3

You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

Ralph Peters

Jul 1, 2021, 2:13:33 PMJul 1
to j2objc-discuss
Thanks Tom. You are of course quite correct, they have different thread ids.

The screen shots don't reflect the actual situation because the breakpoints are not actually occurring on the Java lines indicated. I've noticed that this happens sometimes. So I thought it was breaking as indicated and the different blocks of code were on the same thread, but under the covers the break was happening a bit before that line of java (where it really was on the same thread). I hope that makes sense. Note to self: don't trust the debugger to have actually stopped on the line it shows it has. 

Can I just add my thanks for all the work that you and the team have done on J2ObjC? I've got a cross-platform app, using an MVP architecture. The model and presenter are in Java, with the views in native Android java and Swift. The vast majority of the code is common, thanks to J2ObjC. For me it really is the difference between a having viable cross-platform app and not. 

Best regards,


Tom Ball

Jul 1, 2021, 2:40:52 PMJul 1
Awesome, I'm glad it's working for you.

One point about debuggers is that their line numbers are affected by optimization, which can move code around from the order in which it's written in source code. What we want (usually) is no optimization when debugging, and optimized for release builds. To check in Xcode, select your app target, click on Build Setting, and search for Optimization Level: it should have -Onone or -O0 (dash-capital-o-zero) for Debug builds. If you find line numbers still not lining up with no optimization set, and don't mind sharing the source to recreate the problem, please file an issue.

Reply all
Reply to author
0 new messages