Android线程:是否有必要在“加入”之前等待线程启动?

时间:2015-08-27 01:45:08

标签: android multithreading runnable

我正在实施Android“服务”。在它的“onCreate”中,我想开始并等待另一个线程的完成。 ClientServiceLoop是一个Runnable,在run()中有一个while(true)循环,返回条件简单。

    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

我想知道的是,在调用start()之后,新生成的线程是否已经保证已经调用了Runnable的run()方法?我应该在调用join()之前等待线程启动吗?我无法找到有关保证确切位置的文档。

如果我不调用start(),则将其置于测试中,join()会立即返回。我想知道isAlive()实际上是什么时候设置的。我搜索了Android sdk但找不到nativePeer的设置位置。

-

/**
 * 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);
}

- Android来源

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'layout'=>"{items}{pager}",
    'tableOptions' => ['class' => 'table  table-bordered table-hover'],
    'showFooter'=>false,
    'showHeader' => false,
    'pager' => [
            'firstPageLabel' => 'First',
            'lastPageLabel' => 'Last',
        ],

    'columns' => [
        [   'attribute' => 'iduser.photo',
            'format' => 'html',
            'value'=>  function($data) { return Html::img($data->imageurl) . " <p class='feedback-username'>" . $data->username . "</p>"; },
            'contentOptions'=>['style'=>'max-width: 10px; max-height: 10px'],
        ],

        [   'attribute' => 'KOMENTAR',
            'format' => 'raw',
            'value' => function($model) { return $model->KOMENTAR  ."<br><p class='feedback-date'>". $model->TANGGAL ."</p>";},
        ],

        [   'class' => 'yii\grid\ActionColumn',
            'contentOptions'=>['style'=>'width: 5px;'],
            'template' => '{update} {delete}'
        ],
    ],
]); ?> 

nativePeer设置在哪里?

2 个答案:

答案 0 :(得分:2)

从技术上讲,您可以在致电join之前致电start

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

不要这样做

启动主题后,您可以让onCreate()正常返回,服务不会被销毁。

答案 1 :(得分:1)

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