You are on page 1of 4

6/13/13

Difference between start and run method in Thread Java Tutorial

Javarevisited
Blog about Java Program Tutorial Example How to, Unix Linux commands, Interview Questions, FIX Protocol, Tibco RV tutorials, Equities trading system, MySQL

Java Programer
S A T UR DA Y , MA R C H 24, 2012

Learning Java

Java com

Run
Searc h

Difference between start and run method in Thread Java Tutorial


Recent Posts
Why do one call s t a r tmethod of thread if s t a r t ( )calls r u n ( ) in turn" or "What is difference by calling s t a r t () over r u n ( ) method in java thread" are two widely popular beginner level multithreading interview question. When a Java programmer s t a r t learning Thread, first thing he learns is to implement thread either overriding r u n ( )method of Thread class or implementing Runnable interface and than calling s t a r t ( )method on thread, but with some experience he finds that s t a r t () method calls r u n ( )method internally either by looking API documentation or just poking around, but many of us just dont care at that time until its been asked in Java Interview. In this Java tutorial we will see What is difference between calling s t a r t ( )method and r u n ( ) method for starting Thread in Java. This article is in continuation of my earlier post on Java multi-threading e.g. Difference between Runnable and Thread in Java and How to solve Producer Consumer problem in Java using B l o c k i n g Q u e u e . If you havent read them already you may find them interesting and useful.

How to Generate MD5 checksum for Files in Java How to get current URL, parameters and Hash tag using JQuery and JavaScript Why Favor Composition over Inheritance in Java and Object Oriented Programming How to find CPU and Memory used by Java process in Solaris prstat command example How to Check if Integer Number is Power of Two in Java - 3 examples Java Tips and Best practices to avoid NullPointerException in Java Applications

Difference between s t a r tand r u nin Java Thread


So what is difference between s t a r tand r u nm e t h o d ? Main difference is that when program calls s t a r t () method a new Thread is created and code inside r u n ( )method is executed in new Thread while if you call r u n ( )method directly no new Thread is created and code inside r u n ( )will execute on current Thread. Most of the time calling r u n ( )is bug or programming mistake because caller has intention of calling s t a r t () to create new thread and this error can be detect by many static code coverage tools like findbugs. If you want to perform time consuming task than always call s t a r t () method otherwise your main thread will stuck while performing time consuming task if you call r u n ( )method directly. Another difference between start vs run in Java thread is that you can not call s t a r t ( )method twice on thread object. once started, second call of start() will throw I l l e g a l S t a t e E x c e p t i o n in Java while you can call r u n ( )method twice.

Code Example of s t a r tvs r u nmethod


Here is a simple code example which prints name of Thread which executes r u n ( )method of Runnable task. Its clear that if you call s t a r t ( )method a new Thread executes Runnable task while if you directly call r u n ( )method task, current thread which is main in this case will execute the task. p u b l i cc l a s sS t a r t V s R u n C a l l { p u b l i cs t a t i cv o i dm a i n ( S t r i n ga r g s [ ] ){ / / c r e a t i n gt w ot h r e a d sf o rs t a r ta n dr u nm e t h o dc a l l T h r e a ds t a r t T h r e a d=n e wT h r e a d ( n e wT a s k ( " s t a r t " ) ) ; T h r e a dr u n T h r e a d=n e wT h r e a d ( n e wT a s k ( " r u n " ) ) ; s t a r t T h r e a d . s t a r t ( ) ;/ / c a l l i n gs t a r tm e t h o do fT h r e a d-w i l le x e c u t ei nn e wT h r e a d r u n T h r e a d . r u n ( ) ; / / c a l l i n gr u nm e t h o do fT h r e a d-w i l le x e c u t ei nc u r r e n tT h r e a d } / * *S i m p l eR u n n a b l ei m p l e m e n t a t i o n * / p r i v a t es t a t i cc l a s sT a s ki m p l e m e n t sR u n n a b l e { p r i v a t eS t r i n gc a l l e r ; p u b l i cT a s k ( S t r i n gc a l l e r ) { t h i s . c a l l e r=c a l l e r ; } @ O v e r r i d e p u b l i cv o i dr u n ( ){ S y s t e m . o u t . p r i n t l n ( " C a l l e r :" +c a l l e r+"a n dc o d eo nt h i sT h r e a di se x e c u t e db y :"+T h r e a d . c u r r e n t T h r e a d ( ) . g e t N a m e ( ) ) ;

Follow Us
Follow @javinpaul 2,846 follow ers

Javarevisited on

Follow

+3,116

javarevisited.blogspot.sg/2012/03/difference-between-start-and-run-method.html

1/4

6/13/13
} } } O u t p u t :

Difference between start and run method in Thread Java Tutorial

C a l l e r :s t a r ta n dc o d eo nt h i sT h r e a di se x e c u t e db y:T h r e a d 0 C a l l e r :r u na n dc o d eo nt h i sT h r e a di se x e c u t e db y:m a i n

In Summary only difference between s t a r t () and r u n ( )method in Thread is that s t a r tcreates new thread while run doesn't create any thread and simply execute in current thread like a normal method call. Other Java tutorials on Thread you may like

Javarevisited
How to Stop Thread in Java Difference between ConcurrentHashMap and Hashtable in Java Why wait and notify methods are declared in Object Class? Why wait and notify needs to called from Synchronized Context? Difference between invokeAndWait and InvokeLater in java Swing. Difference between wait and sleep in Java How to write Thread Safe Class in Java How to fix Race conditions in Java with Example Difference between Daemon and non Daemon Thread in Java
F acebook social plugin

Like 4,038 people like Javarevisited.

Recommendd Books Java Programming and Design Pattern Spring Framework Subscribe by email: Please share with your friends if like this article By Javin Paul Golf Swing
You might like:

Subscribe

Sleep

Articles

Business ethics articles

News Articles

Subscribe To This Blog Free

Posts Comments
Followers Join this site
Recommended by

Java Tips and Best practices to avoid NullPointerException in Java Applications Top 15 Java threading interview questions asked in Investment banks Difference between transient and volatile keyword in Java 10 Java String interview Question answers - Advanced

w ith Google Friend Connect

Members (1166) More Posted by Javin Paul at 11:01 PM Labels: core java, core java interview question
+4 Recommend this on Google

Programming Golf Swing

Code Example Sleep Articles

Programm

Java While

Java Programmer News Articles

Business ethics articles

No comments:
Already a member? Sign in

Post a Comment
E n t e ry o u rc o m m e n t . . .

Java Programming Tutorial Start to Run Free Java


Blog Archive 2013 (88) 2012 (218)

Comment as: Google Account Publish Preview

December (52) November (8) October (15) September (8) August (9) July (9) June (12) May (10) April (14)

javarevisited.blogspot.sg/2012/03/difference-between-start-and-run-method.html

2/4

6/13/13

Difference between start and run method in Thread Java Tutorial


March (28) Private in Java: Why should you always keep fields...

Newer Post Subscribe to: Post Comments (Atom)

Home

Older Post

How to Compare two String in Java - String Compari... How to loop ArrayList in Java - Code Example Difference between start and run method in Thread ... Why use PreparedStatement in Java JDBC Example T... SimpleDateFormat in Java is not Thread-Safe Use Ca... How to find file and directory size in Unix with E... Difference between transient and volatile keyword ... How to fix java.io.FileNotFoundException: (Access ... Spring Security Example Tutorial - How to limit nu... What is Daemon thread in Java and Difference to No... How to get ServletContext in Servlet, JSP, Action ... Why character array is better than String for Stor... JDBC - java.lang.ClassNotFoundException: com.mysql... Mixing static and non static synchronized method -... What is GET and POST method in HTTP and HTTPS Prot... What is Static and Dynamic binding in Java with Ex... 10 points on finalize method in Java Tutorial Ex... What is Encapsulation in Java and OOPS with Exampl... 10 example of chmod command in UNIX Linux Top 10 EJB Interview Question and Answer asked in ... java.lang.UnsatisfiedLinkError: no dll in java.lib... How to read Properties File in Java XML and Text... How to add or list certificates from keystore or t... 10 Object Oriented Design Principles Java Programm... java.lang.NoSuchMethodError: main Exception in thr... How to create and execute JAR file in Java Comma... How to format Decimal Number in Java DecimalForm... February (18) January (35) 2011 (145) 2010 (33)

References Java API documentation JDK 6 Spring framework doc Struts ANT Maven

javarevisited.blogspot.sg/2012/03/difference-between-start-and-run-method.html

3/4

6/13/13

Difference between start and run method in Thread Java Tutorial


JDK 7 API MySQL Linux Eclipse Copyright by Javin Paul 2012. Powered by Blogger.

About Me

Privacy Policy

javarevisited.blogspot.sg/2012/03/difference-between-start-and-run-method.html

4/4

You might also like