В чем разница между "Thread.currentThread().getName" и "this.getName"?

Вот код:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;


class UnCatchExceptionThread extends Thread{
    public UnCatchExceptionThread(String name){
        this.setName(name);
    }
    @Override
    public void run() {
        System.out.println("Thread name is: " + this.getName());
        throw new RuntimeException();
    }
}

class UnCatchExceptionHandler implements Thread.UncaughtExceptionHandler{
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("catch "  + e + " from " + t.getName());
    }
}

class HandlerFactory implements ThreadFactory{

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r);
        t.setUncaughtExceptionHandler(new UnCatchExceptionHandler());
        return t;
    }

}
public class CaptureException {

    public int i;
    /**
     * @param args
     */
    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool(new HandlerFactory());
        exec.execute(new UnCatchExceptionThread("Gemoji"));
    }

}

И вывод:

Имя потока: Gemoji
catch java.lang.RuntimeException from Thread-1

If I changed the code

System.out.println("Thread name is: " + this.getName());  

to

System.out.println("Thread name is: " + Thread.currentThread().getName()); 

The output will change to

Thread name is: Thread-1
catch java.lang.RuntimeException from Thread-1

Why?

7
задан PeaceMaker 28 December 2011 в 14:57
поделиться