创建和启动线程

package cc.nn;
class TwoThread extends Thread{
    private Thread creatorThread ;
    public TwoThread(){
        creatorThread = Thread.currentThread() ;
        
    }
    
    public void run(){
        for( int i = 0 ; i < 10 ; i ++){
            printMsg() ;
        }
    }
    
    public void printMsg(){
        Thread t = Thread.currentThread() ;
        if(t == creatorThread) {
            System.out.println("Creat Thread");
        }else if(t == this){
            System.out.println("new THread") ;
        }
    }
}
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TwoThread twoThread = new TwoThread() ;
        twoThread.start() ;
        
        for( int i = 0 ; i < 10 ; i ++){
            twoThread.printMsg() ;
        }
        
    }

}

Thread.currentThread() 获取线程实例..

 

 

在线程启动前setName() ;

    TwoThread twoThread = new TwoThread() ;
            twoThread.setName("hello") ;
            twoThread.start() ;

 

Thread.currentThread()  返回当前线程实例

setName() getName()      设置线程名字,返回线程名字

isAlive()          判断线程是否存活

 sleep()          暂停

posted @ 2016-03-01 17:56  式微胡不归  阅读(146)  评论(0编辑  收藏  举报