新线程上的getName()返回' main'

时间:2017-06-23 20:51:53

标签: android android-log android-thread

我在onCreate()

中的MainActivity初始化并运行新主题
@Override
protected void onCreate(Bundle savedInstanceState) {

// ... code

// New thread
new Thread(){
        @Override
        public void run(){

            // Output is 'TAG: main'
            Log.v("TAG",Thread.currentThread().getName());
        }
    }.run();
}

为什么这一行TAG: main的输出?

// Output is 'TAG: main'
Log.v("TAG",Thread.currentThread().getName());

这个帖子不应该是新主题吗?

如果我将无限while循环粘贴到线程中,我的应用程序会锁定,表明这个新线程确实是主线程。

@Override
protected void onCreate(Bundle savedInstanceState) {

// ... code

// New thread
new Thread(){
        @Override
        public void run(){

            // Output is 'TAG: main'
            Log.v("TAG",Thread.currentThread().getName());

            // Locks up the main thread apparently
            while(true) {}
        }
    }.run();
}

我只是不明白。我不是在创建一个新线程,为什么?

0 个答案:

没有答案
相关问题