Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Runnable和thread创建线程的区别?_Java - Fatal编程技术网

Java 使用Runnable和thread创建线程的区别?

Java 使用Runnable和thread创建线程的区别?,java,Java,这段代码运行良好,但如果我在第6行中使用构造函数Threadname而不是Threadthis,它不工作,我只想知道是什么造成了差异 public class threadtest implements Runnable{ Thread t; public threadtest(String name) { System.out.println("satheesh"); Thread t=new Thread(this,name);

这段代码运行良好,但如果我在第6行中使用构造函数Threadname而不是Threadthis,它不工作,我只想知道是什么造成了差异

public class threadtest implements Runnable{
    Thread t;

    public threadtest(String name)
    {
        System.out.println("satheesh");
        Thread t=new Thread(this,name);
        t.start();
        t=null;
        //System.out.println(this+"\n"+t);
    }

    public void run(){
        System.out.println("satheesh");
        for(int i=0;i<=10;i++)
        {
            try{
                System.out.println("satheesh");
                Thread.sleep(1000);
                System.out.print(Thread.currentThread());
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }

    public static void main(String args[])
    {
        threadtest ob=new threadtest("satheesh");       
    }
}
编写新的Threadsomename会创建一个什么都不做的线程。 因为您从未为它运行提供任何东西

写入新的Threadsomename会创建一个什么都不做的线程。
由于您从未为其运行提供任何信息

,因此您的问题与此问题相同:


另一个好的信息来源是coderanch.com上的这个帖子

你的问题与这个问题相同:


另一个好的信息来源是coderanch.com上的这个线程。创建线程有两种方法:

子类线程,重写run,然后创建子类的实例

扩展Runnable并将其交给线程运行

您的代码执行2-您实现了Runnable,因此您必须将其交给一个线程来运行它

如果我在第6行中使用构造器Threadname而不是Threadthis,它不起作用,我只想知道是什么造成了不同

public class threadtest implements Runnable{
    Thread t;

    public threadtest(String name)
    {
        System.out.println("satheesh");
        Thread t=new Thread(this,name);
        t.start();
        t=null;
        //System.out.println(this+"\n"+t);
    }

    public void run(){
        System.out.println("satheesh");
        for(int i=0;i<=10;i++)
        {
            try{
                System.out.println("satheesh");
                Thread.sleep(1000);
                System.out.print(Thread.currentThread());
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }

    public static void main(String args[])
    {
        threadtest ob=new threadtest("satheesh");       
    }
}
区别在于:

Thread t=新的Threadthis,name

创建一个新线程,该线程被赋予运行权限,以便在启动时运行该线程

螺纹t=新螺纹名称

创建一个新线程,但该线程没有任何可运行性。因此,线程在启动时不执行任何操作


创建线程有两种方法:

子类线程,重写run,然后创建子类的实例

扩展Runnable并将其交给线程运行

您的代码执行2-您实现了Runnable,因此您必须将其交给一个线程来运行它

如果我在第6行中使用构造器Threadname而不是Threadthis,它不起作用,我只想知道是什么造成了不同

public class threadtest implements Runnable{
    Thread t;

    public threadtest(String name)
    {
        System.out.println("satheesh");
        Thread t=new Thread(this,name);
        t.start();
        t=null;
        //System.out.println(this+"\n"+t);
    }

    public void run(){
        System.out.println("satheesh");
        for(int i=0;i<=10;i++)
        {
            try{
                System.out.println("satheesh");
                Thread.sleep(1000);
                System.out.print(Thread.currentThread());
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }

    public static void main(String args[])
    {
        threadtest ob=new threadtest("satheesh");       
    }
}
区别在于:

Thread t=新的Threadthis,name

创建一个新线程,该线程被赋予运行权限,以便在启动时运行该线程

螺纹t=新螺纹名称

创建一个新线程,但该线程没有任何可运行性。因此,线程在启动时不执行任何操作


使用runnable接口创建线程,如果使用runnable接口,则在线程构造函数中传递runnable对象引用和线程名称。当使用threadname时,它不称为start,但在创建threadthis时,命名它完全满足runnable thread和start的要求。

使用runnable接口创建线程,如果使用runnable接口,则在线程构造函数中传递runnable对象引用和线程名称。当使用threadname时,它不称为start,但当创建threadthis时,命名它完全满足了runnable thread和start的要求。

对于不同的线程构造函数的作用有一个简单且可以理解的误解。有两名施工人员受到质疑:

创建一个线程,将其名称指定给字符串,并指定它应该运行Runnable

使用的特殊魔术参数调用通用构造函数。这将创建一个新线程,但它将在线程中运行该方法,而不是任何提供的可运行方法。因此,如果调用t.start,它将调用线程的run方法

因此,只需简单地重写代码即可满足您的需求:

public class threadtest extends Thread { // [sic] on the capitalization

    public threadtest(String name) {
        System.out.println("satheesh");
    }

    public void run() {
        System.out.println("satheesh");
        for(int i=0;i<=10;i++) {
        try {
            System.out.println("satheesh");
            Thread.sleep(1000);
            System.out.print(Thread.currentThread());
        } catch(Exception e) {
            System.out.println(e);
        }
    }

    public static void main(String args[]) {
        threadtest ob = new threadtest("satheesh");
        // The following will call the correct run method now
        ob.start();
    }

}

对于不同的线程构造函数所做的工作,存在一个简单且可以理解的误解。有两名施工人员受到质疑:

创建一个线程,将其名称指定给字符串,并指定它应该运行Runnable

使用的特殊魔术参数调用通用构造函数。这将创建一个新线程,但它将在线程中运行该方法,而不是任何提供的可运行方法。因此,如果调用t.start,它将调用线程的run方法

因此,只需简单地重写代码即可满足您的需求:

public class threadtest extends Thread { // [sic] on the capitalization

    public threadtest(String name) {
        System.out.println("satheesh");
    }

    public void run() {
        System.out.println("satheesh");
        for(int i=0;i<=10;i++) {
        try {
            System.out.println("satheesh");
            Thread.sleep(1000);
            System.out.print(Thread.currentThread());
        } catch(Exception e) {
            System.out.println(e);
        }
    }

    public static void main(String args[]) {
        threadtest ob = new threadtest("satheesh");
        // The following will call the correct run method now
        ob.start();
    }

}

首先,我们应该知道ThreadString s与ThreadRunnable r,String s的不同用途

不同的是ThreadString s,我们将括号中的值发送给实现Runnable的构造函数,而ThreadRunnable r,String s,我们将字符串s的线程名称发送给实现Runnable的线程构造函数

这里的代码与通过线程Runnable r、字符串s实现Runnable的代码相同

public class threadtest implements Runnable{
Thread t;
threadtest th;

public threadtest(){}

public threadtest(String name)
{
System.out.println("satheesh");
Thread t=new Thread(th, name); //satheesh,name of thread, gave to name
t.start(); //2nd thread that will start run() method in void run()
//t=null;
//System.out.println(this+"\n"+t);
}
public void run(){
 System.out.println("satheesh");
for(int i=0;i<=10;i++)
{
try{
System.out.println("satheesh");
Thread.sleep(1000);
System.out.print(Thread.currentThread());
}
catch(Exception e)  { System.out.println(e); }
}
}
public static void main(String args[]){

//ob is Runnable object that will send his empty value ()
threadtest ob = new threadtest(); //to default constructor threadtest() above

//satheesh is name of main thread that we will send to String name in Thread t=new Thread(th, name);
Thread th = new Thread(ob, "satheesh");
th.start();  //1st thread that will instruct to send satheesh
}
}

首先,我们应该知道ThreadString s与ThreadRunnable r,String s的不同用途

不同的是ThreadString s,我们将括号中的值发送给实现Runnable的构造函数,而ThreadRunnable r,String s,我们将字符串s的线程名称发送给实现Runnable的线程构造函数

这里的代码与通过线程Runnable r、字符串s实现Runnable的代码相同

public class threadtest implements Runnable{
Thread t;
threadtest th;

public threadtest(){}

public threadtest(String name)
{
System.out.println("satheesh");
Thread t=new Thread(th, name); //satheesh,name of thread, gave to name
t.start(); //2nd thread that will start run() method in void run()
//t=null;
//System.out.println(this+"\n"+t);
}
public void run(){
 System.out.println("satheesh");
for(int i=0;i<=10;i++)
{
try{
System.out.println("satheesh");
Thread.sleep(1000);
System.out.print(Thread.currentThread());
}
catch(Exception e)  { System.out.println(e); }
}
}
public static void main(String args[]){

//ob is Runnable object that will send his empty value ()
threadtest ob = new threadtest(); //to default constructor threadtest() above

//satheesh is name of main thread that we will send to String name in Thread t=new Thread(th, name);
Thread th = new Thread(ob, "satheesh");
th.start();  //1st thread that will instruct to send satheesh
}
}

您的问题没有意义。请澄清并检查此线程:您可能会发现阅读线程的代码很有趣,特别是run方法。您的问题没有意义。请澄清并检查此线程:您可能会发现阅读线程i的代码
有趣的是,特别是run方法。标题类似,但问题似乎不同。@jzd上传者在我发布后改变了他的问题。现在完全不同的问题:/。标题类似,但问题似乎不同。@jzd上传者在我发布后更改了他的问题。现在完全不同的问题:/。请看这个:请看这个: