为什么定义的run()方法不是在调用Thread调用时调用的?

时间:2016-06-16 19:40:24

标签: java android multithreading

我只是在围绕Thread类试验一些代码而且我遇到了困难,首先看看我的代码

class ThreadExample implements Runnable
{
String threadName;
Thread thread;
public ThreadExample()
    {
    thread=new Thread();
    thread.start();
    }
public void run()
    {
    System.out.println("Thread "+getThreadName()+" is being executed");
    }
void setThreadName(String string)
    {
    threadName=string;
    thread.setName(string);
    }
String getThreadName()
    {
    return thread.getName();
    }
public static void main(String string[]) throws InterruptedException
    {
    ThreadExample threadExample= new ThreadExample();
    threadExample.setThreadName("Thread !");
    //threadExample=new ThreadExample();
    //threadExample.setThreadName("Thread 2");
    //threadExample=new ThreadExample();
    //threadExample.setThreadName("Thread 3");
    Thread.sleep(500);
    }
}

我认为这段代码非常简单,每个人都应该有我的意图虽然当我运行这个程序它甚至没有调用run()方法就完成了,即使我让主线程等待一段时间直到孩子ThreadExample的线程完成。如果我忘记了一些事情,我很新闻。提前致谢。

4 个答案:

答案 0 :(得分:5)

您创建了Runnable类型,但从未将其传递给线程上下文。您将要将其添加到线程。我会做类似的事情:

String threadName;
Thread thread;
public ThreadExample() {
    thread=new Thread(this);
}

public void startThread() {
   thread.start();
} 

Thread类接受Runnable作为参数。

答案 1 :(得分:0)

要运行此实现类,请创建一个Thread对象,将Runnable实现类对象传递给其构造函数。在线程类上调用start()方法以开始执行run()方法 您错过了以下两行:

  

Thread thread1 = new Thread(threadExample);
          thread1.start();

   class ThreadExample implements Runnable
    {
    String threadName;
    Thread thread;
    public ThreadExample()
        {           
        }
    public void run()
        {
        System.out.println("Thread "+getThreadName()+" is being executed");
        }
    void setThreadName(String string)
        {
        threadName=string;
        thread.setName(string);
        }
    String getThreadName()
        {
        return thread.getName();
        }
    public static void main(String string[]) throws InterruptedException
        {
        ThreadExample threadExample= new ThreadExample();
        threadExample.setThreadName("Thread !");
        //threadExample=new ThreadExample();
        //threadExample.setThreadName("Thread 2");
        //threadExample=new ThreadExample();
        //threadExample.setThreadName("Thread 3");
        Thread.sleep(500);
        Thread thread1 = new Thread(threadExample);
        thread1.start();
        }
    }

答案 2 :(得分:0)

你永远不会调用run()方法。你宁愿调用start,你已经在ThreadExample()构造函数中做了,但是我会解释它有一些错误:

在java中,你有2个选项来处理Threads。首先是从Thread类继承,因此可以从中调用start()方法,并执行run()中的代码。第二个选项是创建一个Runnable,它似乎是您选择的选项,但要运行它,您必须创建一个这样的线程:

ThreadExample runnable = new ThreadExample();
Thread myThread = new Thread(threadExample);

然后,当你准备好开始你的线程时,你可以调用myThread.start();

答案 3 :(得分:0)

正如John Vint所指出的,Thread类需要一个Runnable目标。我稍微编辑了你的程序:

public class NewThreadExample implements Runnable{

    String threadName;

    public String getThreadName() {
        return threadName;
    }

    public void setThreadName(String threadName) {
        this.threadName = threadName;
    }

    public static void main(String[] args) throws InterruptedException {

        NewThreadExample threadTarget = new NewThreadExample();
        threadTarget.setThreadName("Dushyant");

        Thread thread = new Thread(threadTarget);
        System.out.println("Thread created and going to start");
        thread.start();
        System.out.println("Thread sleeping");
        Thread.sleep(2000);
        System.out.println("Program done");
    }

    @Override
    public void run() {
        System.out.println(this.getThreadName() + " is running...");
    }

}

给出

  

线程创建并开始

     

线程睡眠

     

Dushyant正在运行......

     

程序完成