简体   繁体   中英

Difference in creation of thread using Runnable and Thread?

This code is working fine but if I use the constructor Thread(name) in 6th line instead of Thread(this,name) it is not working I just want to know what makes the difference?

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");       
    }
}

There's two ways to create a thread:

  1. Subclass Thread , override run , and then create an instance of your subclass

  2. Extend Runnable and give it to a thread to "run" it

Your code does #2 - you implemented Runnable , so you must give it to a thread to "run" it.

if I use the constructor Thread(name) in 6th line instead of Thread(this,name) it is not working I just want to know what makes the difference?

The difference is that:

  • Thread t= new Thread(this,name);

    Creates a new thread that is given your Runnable ( this ) to run when it is started.

  • Thread t= new Thread(name);

    Creates a new thread is not given any Runnable to run. So the thread does nothing when it is started.

Writing new Thread("somename") creates a thread that won't do anything.
(since you never provided anything for it to run)

我们使用runnable接口创建线程,如果我们使用runnable接口,则在线程构造函数中,我们传递runnable对象引用和线程名称。当您使用thread(name)时,它不称为start(),但是当您创建thread(this时,名称),它满足了可运行线程的需求并启动了start()。

There's a simple (and understandable) misunderstanding of what the different Thread constructors do. There are two constructors in question:

  1. Thread(Runnable, String) creates a Thread , assigns its name to the String and specifies that it should run the Runnable .

  2. Thread(String) calls the general constructor with special magic arguments of Thread (null, null, name) . This creates a new Thread but it will run the method in the Thread , not any provided Runnable . As a result, if you call t.start() , it's going to call the Thread's run() method.

So, a simple rewrite of the code gets you what you want:

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();
    }

}

1st, we should knew Thread(String s) vs Thread(Runnable r, String s) different purpose.

The different is Thread(String s) we sent "value" in bracket () to constructor that implements Runnable but Thread(Runnable r, String s) we give a thread name to String s to the thread constructor that implements Runnable.

Here the same code that implements Runnable via Thread (Runnable r, String s).

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
}
}

output:

satheesh
satheesh
Thread[satheesh,5,main]satheesh
Thread[satheesh,5,main]satheesh
Thread[satheesh,5,main]satheesh
Thread[satheesh,5,main]satheesh
Thread[satheesh,5,main]satheesh
Thread[satheesh,5,main]satheesh
Thread[satheesh,5,main]satheesh
Thread[satheesh,5,main]satheesh
Thread[satheesh,5,main]satheesh
Thread[satheesh,5,main]satheesh
Thread[satheesh,5,main]satheesh
BUILD SUCCESSFUL (total time: 11 seconds)

Your question is the same as this question: "implements Runnable" vs. "extends Thread"

Another good source of information is this thread on coderanch.com http://www.coderanch.com/t/233370/threads/java/Thread-vs-Runnable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM