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);
}开发者_C百科
}
}
public static void main(String args[])
{
threadtest ob=new threadtest("satheesh");
}
}
There's two ways to create a thread:
Subclass
Thread
, overriderun
, and then create an instance of your subclassExtend
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 ofThread(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)
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
u r using runnable interface for creating thread,if we use runnable interface then in thread constructor we pass runnable object refernce and the name of thread.when u use thread(name),its not called start() but when u create thread(this ,name) it fullfill the requirement of runnable thread and start start().
There's a simple (and understandable) misunderstanding of what the different Thread constructors do. There are two constructors in question:
Thread(Runnable, String) creates a
Thread
, assigns its name to theString
and specifies that it should run theRunnable
.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 theThread
, not any providedRunnable
. As a result, if you callt.start()
, it's going to call the Thread'srun()
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)
精彩评论