run vs start java

Per StackOverflow



public class StartVSRun {public static void main(String[] args) { Me  me2=new Me(); Thread me=new Thread(me2); /* me.start(); me.start(); class java.lang.Thread Exception in thread “main” java.lang.IllegalThreadStateException at java.lang.Thread.start(Unknown Source) at StartVSRun.main( Picked up _JAVA_OPTIONS: -Xmx512M*/;; System.out.println(“—————“); me.start(); //me.start(); //me.start(); //me.start();
class Me implements Runnable{
@Override public void run() { System.out.println(Thread.currentThread().getName()); } }

Picked up _JAVA_OPTIONS: -Xmx512M


The difference is that Thread.start() starts a thread, while just calls a method.

Here run() always results in main ,start will gives thread-0,thread-1 etc.


up vote202down voteaccepted

First example: No multiple threads. Both execute in single (existing) thread. No thread creation.

R1 r1 = new R1();
R2 r2 = new R2();

r1 and r2 are just two different objects of the class that implements the Runnable interfaces which have that run() method. When you call you are executing it in the current thread.

Second example: Two separate threads.

Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);

t1 and t2 are the objects of the class Thread. When you call t1.start(), it starts a new thread and calls the run() method of r1 internally to execute it within that new thread.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s