代码:

package com.hello.test4;

import android.app.Activity;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;

public class Hello4Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        long id1 = Thread.currentThread().getPriority();
        Log.v("Hello4Activity", "mainid=="+Thread.currentThread().getPriority());
        t1.setPriority(Thread.MAX_PRIORITY);
        t1.start();
        try {
            t1.join();
            Log.v("Hello4Activity", "1id=="+Thread.currentThread().getPriority());
            Log.v("Hello4Activity", "t1 --id=="+t1.getPriority());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //t2.setPriority(Thread.MIN_PRIORITY);
        t2.setPriority(7);
        t2.start();
        try {
            t2.join();
            Log.v("Hello4Activity", "2id=="+Thread.currentThread().getPriority());//在main thread里面打印,打印的是main thread的priority
            Log.v("Hello4Activity", "t2 --id=="+t2.getPriority());//指定了t2,打印t2的priority。
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    Thread t1 = new Thread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            Log.v("Hello4Activity", "#####t1");
            Log.v("Hello4Activity", "t1id=="+Thread.currentThread().getPriority());
        }

    });
    Thread t2 = new Thread(new Runnable() {//一个线程只存在于其run方法中!

        public void run() {
            // TODO Auto-generated method stub
            Log.v("Hello4Activity", "#####t2");
            Log.v("Hello4Activity", "t2id=="+Thread.currentThread().getPriority());
        }

    });
}

 

 

在main thread里面打印

Log.v("Hello4Activity", "id=="+Thread.currentThread().getPriority());

得到的是main thread的priority,在其他线程的run方法里打印此句,得到的是此线程的priority

 

打印结果:

 

01-01 17:10:43.656: V/Hello4Activity(1640): mainid==5
01-01 17:10:43.656: V/Hello4Activity(1640): #####t1
01-01 17:10:43.656: V/Hello4Activity(1640): t1id==10     //在Thread t1中打印

01-01 17:10:43.656: V/Hello4Activity(1640): 1id==5        //在main thread中打印
01-01 17:10:43.656: V/Hello4Activity(1640): t1 --id==10    //指定t1 thread的priority打印
01-01 17:10:43.657: V/Hello4Activity(1640): #####t2
01-01 17:10:43.657: V/Hello4Activity(1640): t2id==7
01-01 17:10:43.657: V/Hello4Activity(1640): 2id==5
01-01 17:10:43.657: V/Hello4Activity(1640): t2 --id==7

 

 

若不主动设置线程优先级,那么这些线程的优先级都是相同的,都是normal,5,log信息如下:

01-01 17:17:29.754: V/Hello4Activity(1898): mainid==5
01-01 17:17:29.755: V/Hello4Activity(1898): #####t1
01-01 17:17:29.755: V/Hello4Activity(1898): t1id==5
01-01 17:17:29.756: V/Hello4Activity(1898): 1id==5
01-01 17:17:29.756: V/Hello4Activity(1898): t1 --id==5
01-01 17:17:29.757: V/Hello4Activity(1898): #####t2
01-01 17:17:29.757: V/Hello4Activity(1898): t2id==5
01-01 17:17:29.758: V/Hello4Activity(1898): 2id==5
01-01 17:17:29.758: V/Hello4Activity(1898): t2 --id==5

posted on 2012-04-11 10:07  snowdrop  阅读(398)  评论(0编辑  收藏  举报