需要在java中理解线程(this,ThreadName)吗?

时间:2012-06-05 12:31:43

标签: java multithreading

public class GetCurrentThread implements Runnable {
   Thread th;

   public GetCurrentThread(String threadName) {
      th = new Thread(this,threadName); //<----DOUBT 
      System.out.println("get threadname "+th.getName());
      th.start();
   }

  public void run() {
     System.out.println(th.getName()+" is starting.....");
     System.out.println("Current thread name : " + Thread.currentThread().getName());
  }

  public static void main(String args[]) {
     System.out.println("Current thread name : " + Thread.currentThread().getName());
     new GetCurrentThread("1st Thread");
     //new GetCurrentThread("2nd Thread");
   }
}

有人可以解释上面代码的第二行是做什么的吗? 我对&#34; th = new Thread(this,threadName)&#34;的理解是,它将创建具有给定名称的线程对象;让我们说出名字&#34; 1st Thread&#34;。现在,什么&#34;这个&#34;关键字在这里做什么?因为当我删除它并尝试获取线程的名称时,我得到了没有问题的名称,但它从未开始运行()。有人可以用简单的术语解释,而不是用一行回答。我非常感谢大家的帮助。

5 个答案:

答案 0 :(得分:3)

  

th = new Thread(this,threadName); //&lt; ---- DOUBT

您的第二行正在构建一个新的Thread对象,其中GetCurrentThread类作为目标(this),并且线程名称为"threadName"。您的this可以成为目标,因为它实现了Runnable。在Thread类的内部,当线程启动时,它调用Thread.run(),它执行:

public void run() {
    if (target != null) {
        target.run();
    }
}

您的GetCurrentThread是目标,因为您将this传递给构造函数。实际上它应该被称为GetCurrentThread,因为它不是一个线程。构建Thread之后的两行,开始运行:

  

th.start();

start()方法执行创建单独的工作本机线程的实际工作。该主题的第一件事是在run()类中调用GetCurrentThread方法。

作为评论,通常在其构造函数中将{em>不建议用于start()本身的类。有固有的竞争条件会导致问题。最好在start()

上使用GetCurrentThread方法
/** start the underlying thread */
public void start() {
   th.start();
}

你的主要看起来像:

public static void main(String args[]) {
   System.out.println("Current thread name : " + Thread.currentThread().getName());
   GetCurrentThread thread1 = new GetCurrentThread("1st Thread");
   thread1.start();
}

答案 1 :(得分:1)

Thread的构造函数的第一个参数是一个类型实现Runnable的对象,它正是this所代表的。注意类声明:

public class GetCurrentThread implements Runnable

因此,当线程启动时,执行当前实例的run方法(this)。

无论如何,我尽量避免使用这种代码。这只会导致混乱。

答案 2 :(得分:1)

试试这个,

th = new Thread(this,threadName);

在上面的行"this" keyword中将表示具有implemented Runnable Interface的类的对象。

现在我将尝试更详细地向您展示:

public class A implements Runnable{

   public void run(){

                // some work
        }


}


public class B {


public static void main(String[] args){

A a = new A;
Thread t = new Thread(a);// a is an obj ref variable of class A which implements Runnable
t.start();

 }

}

答案 3 :(得分:0)

this这里是调用run方法的对象,在本例中是GetCurrentThread类的实例。在您th.start()之后,无法保证线程立即开始执行。这里发生的是主线程在看到run方法中的输出行之前完成执行。在main中创建对象后添加th.join(),等待新创建的线程完成:

public class GetCurrentThread implements Runnable {
    Thread th;

    public Thread getThread(){
        return th;
    }

    public GetCurrentThread(String threadName) {
        th = new Thread(this, threadName); // <----DOUBT
        System.out.println("get threadname " + th.getName());
        th.start();
    }

    public void run() {
        System.out.println(th.getName() + " is starting.....");
        System.out.println("Current thread name : "
                + Thread.currentThread().getName());
    }

    public static void main(String args[]) {
        System.out.println("Current thread name : "
                + Thread.currentThread().getName());
        GetCurrentThread currentThread = new GetCurrentThread("1st Thread");
        // new GetCurrentThread("2nd Thread");
        try {
            Thread thread = currentThread.getThread();
            if (thread != null) {
                thread.join();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

你得到的输出是:

Current thread name : main
get threadname 1st Thread
1st Thread is starting.....
Current thread name : 1st Thread

答案 4 :(得分:0)

您指的是构造函数Thread(Runnable target, String name),其中第一个参数target是具有Runnable方法的任何run()实例。

在这种特殊情况下,target对象是非常类GetCurrentThread本身(它是Runnable)的实例,其中创建并启动了线程。因此this代表GetCurrentThread

的实例

Runnable.run()方法实际上是一个线程(由Thread对象表示)要执行的任务。如果传递null而不是它,则不会执行任何线程要执行的任务,因此不会调用run()方法。