Java 为什么线程调用start时不调用定义的run()方法?

Java 为什么线程调用start时不调用定义的run()方法?,java,android,multithreading,Java,Android,Multithreading,我只是在试验线程类的一些代码,我遇到了一些问题,首先看看我的代码 class ThreadExample implements Runnable { String threadName; Thread thread; public ThreadExample() { thread=new Thread(); thread.start(); } public void run() { System.out.println("Thread "+getT

我只是在试验线程类的一些代码,我遇到了一些问题,首先看看我的代码

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完成。我是新来的,如果我忘了什么,很抱歉。提前谢谢

您创建了一个
Runnable
类型,但从未将其传递到线程上下文中。您需要将其添加到线程中。我会这样做:

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

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

Thread
类接受一个
Runnable
作为参数。

您创建了一个
Runnable
类型,但从未将其传递到线程上下文中。您需要将其添加到线程中。我会这样做:

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

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

Thread
类接受一个
Runnable
作为参数。

要运行这个实现类,创建一个Thread对象,将Runnable实现类对象传递给它的构造函数。对thread类调用start()方法以开始执行run()方法。
您错过了以下两行:

螺纹1=新螺纹(螺纹示例)
thread1.start()


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

螺纹1=新螺纹(螺纹示例)
thread1.start()

您从不调用run()方法。您更愿意调用start,您已经在ThreadExample()构造函数中执行了该操作,但它有一些错误,我将对此进行解释:

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

ThreadExample runnable = new ThreadExample();
Thread myThread = new Thread(threadExample);
然后可以调用
myThread.start()当您准备好启动线程时。

您从不调用run()方法。您更愿意调用start,您已经在ThreadExample()构造函数中执行了该操作,但它有一些错误,我将对此进行解释:

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

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

然后可以调用
myThread.start()当您准备好启动线程时。

正如John Vint所指出的,thread类需要一个可运行的目标。我稍微编辑了一下您的程序:

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正在运行

程序完成


正如John Vint指出的,Thread类需要一个可运行的目标。我稍微编辑了一下您的程序:

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正在运行

程序完成


@苏莱曼尼迪我不这么认为,但如果你能详细阐述你的观点那就太好了。@苏莱曼尼迪我不这么认为,但如果你能详细阐述你的观点那就太好了。是的,兄弟,我忘了补充这个。正如我说过的,我是线程新手,所以我需要时间来记住这样的事情。谢谢你,兄弟。祝你好运。十分钟后我会接受这个答案。@DushyantSuthar我很乐意,很高兴能帮上忙。是的,兄弟,我忘了加上这个。正如我说过的,我是线程新手,所以我需要时间来记住这样的事情。谢谢你,兄弟。祝你好运。我将在10分钟后接受这个答案。@DushyantSuthar我很高兴,很乐意帮助。没有兄弟看你答案下面的答案。没有兄弟看你答案下面的答案。看一看已接受的答案。好的,谢谢你的回答,不过你已经足够接近了。看看你接受的答案。好的,谢谢你的回答,不过你已经足够接近了。是的,这也解决了问题,但我在构造函数中寻找代码,所以我不想在main方法中创建线程。你的演讲很好。所有最好的朋友。是的,它也解决了问题,但我在构造函数中寻找代码,所以我不想在主方法中创建线程。你的演讲很好。所有最好的朋友。