我有个疑问,集成Runnable接口的类,再通过Thread类创建线程,为什么this不是指调用start方法的thread对象,而是实现Runnable的类的对象呢?如果是指下面的这个Threst对象,那多个对象如何区分呢
- class Threst implements Runnable{
- private int sum = 100;
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while(true){
- if(sum > 0){
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread().getName() + " " + (sum--));
- System.out.println(this.getClass()); //这里的this为什么不是Thread对象
- }else{
- break;
- }
- }
- }
-
- }
复制代码
- public class ThreadTest {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Threst thr = new Threst();
- Thread th1 = new Thread(thr);
- Thread th2 = new Thread(thr);
- th1.start(); //这个地方明明是th1调用的start方法,为什么上面的this却是Threst
- th2.start();
- }
- }
复制代码 |