2015-08-27 39 views
3

我正在实施Android“服务”。在其“onCreate”中,我想开始并等待另一个线程的完成。 ClientServiceLoop是run()中带有while(true)循环的Runnable,具有简单的返回条件。Android线程:是否需要等待线程在“加入”之前启动?

@Override 
public void onCreate() { 
    super.onCreate(); 
    mClientServiceLoopThread = new Thread(mClientServiceLoop = new ClientServiceLoop(), 
      "ClientServiceLoop"); 
    mClientServiceLoopThread.start(); 
    try { 
     mClientServiceLoopThread.join(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
} 

我想知道是,以后我叫start()方法,是保证已经呼吁Runnable的run()方法已经是新生成的线程?我应该在调用join()之前等待线程启动吗?我无法找到有关确切地址保证的文件。


对此进行测试,如果我不调用start(),join()会立即返回。我想知道的是何时isAlive()被实际设置。我搜索了Android SDK,但无法找到nativePeer设置的位置。

-

mClientServiceLoopThread = new Thread(mClientServiceLoop = new ClientServiceLoop(), 
      "ClientServiceLoop"); 
    boolean b = mClientServiceLoopThread.isAlive(); // false 
    try { 
     mClientServiceLoopThread.join(); // internally just while(isAlive)...so returns immediately 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
    mClientServiceLoopThread.start();//FIXME TESTING ONLY 

- Android的源

/** 
* Blocks the current Thread (<code>Thread.currentThread()</code>) until 
* the receiver finishes its execution and dies. 
* 
* @throws InterruptedException if the current thread has been interrupted. 
*   The interrupted status of the current thread will be cleared before the exception is 
*   thrown. 
* @see Object#notifyAll 
* @see java.lang.ThreadDeath 
*/ 
public final void join() throws InterruptedException { 
    synchronized (lock) { 
     while (isAlive()) { 
      lock.wait(); 
     } 
    } 
} 


/** 
* Returns <code>true</code> if the receiver has already been started and 
* still runs code (hasn't died yet). Returns <code>false</code> either if 
* the receiver hasn't been started yet or if it has already started and run 
* to completion and died. 
* 
* @return a <code>boolean</code> indicating the liveness of the Thread 
* @see Thread#start 
*/ 
public final boolean isAlive() { 
    return (nativePeer != 0); 
} 

其中是nativePeer设置??

回答

0

好吧,我明确地跟踪了这一点。看起来nativeCreate()在pthread_create被调用之前设置了nativePeer。由于pthread_create()不保证线程在返回时启动,我不确定Android是否采用相同的方式。看起来他们已经处理了这个问题。所以一旦start()被调用join()将被保证等待。但join()不会等待,除非start()被调用。

https://android.googlesource.com/platform/art/+/3c50a4b4ba6d7d9369ee9a0bd6d30bf4c9c79bb0/src/thread.cc

// Thread.start is synchronized, so we know that nativePeer is 0, and know that we're not racing to 
// assign it. 
env->SetIntField(java_peer, WellKnownClasses::java_lang_Thread_nativePeer, 
       reinterpret_cast<jint>(child_thread)); 
... 
int pthread_create_result = pthread_create(&new_pthread, &attr, Thread::CreateCallback, child_thread); 
1

当主线程调用mClientServiceLoopThread.join();它将停止运行并等待mClientServiceLoopThread线程完成,因此您可以安全地调用start然后加入。

+0

好吧,无论答案是有意义的。谢谢! –

2

技术上,您可致电start致电join

这里的问题是,默认情况下,服务默认在应用程序的主线程(UI线程)上执行代码。调用join将阻止您的UI线程,并使您的应用完全无响应。

不要这样做

你可以让onCreate()在你启动线程后正常返回,服务不会被销毁。

+0

我决定把这个测试。问题是,看起来像join()会立即返回,如果没有开始。 (更新的原始文章) –