The difference between the Start method and the Run method in Threads-java Tutorial

Source: Internet
Author: User
Tags static class thread class
The difference between the Start method and the Run method in Threads-java Tutorial


In the thread, if the Start method calls the Run method sequentially, why do we choose to invoke the Start method?  Or the difference between calling the Start method and the Run method in a Java thread. These two issues are two very popular beginner-level multithreaded interview questions. When a Java programmer starts learning threads, they first learn to inherit the thread class, overload the Run method, or implement the Runnable interface, implement the Run method, and then invoke the thread instance's Start method. But when he has some experience, he sees the API documentation or other ways that the Start method calls the Run method inside, but many of us know the importance of the problem when they are asked in the interview. In this Java tutorial, we will understand the different places where the Start method and the Run method are invoked when the thread is opened in Java


This article is the next part of some of the articles that we've been publishing on Java multithreading, e.g. difference between Runnable and Thread in Java and I to solve Producer Consumer prob Lem in Java using Blockingqueue. If you haven't read them yet, you'll probably find them interesting and useful.


The difference between start and run in Java threads
The main difference between the start and run methods is that when the program calls the Start method, a new thread is created, and the code in the Run method will run on a new thread, but when you call the Run method directly, the program does not create a new thread, and the code inside the Run method runs on the current thread. In most cases, calling the Run method is a bug or a mistake. Because the caller's intention is to invoke the Start method to open a new thread, this error can be detected by many static code coverage tools, such as with Fingbugs. If you want to run a task that takes a lot of time, you'd better use the Start method, otherwise your main thread will be stuck when you call the Run method. Another difference is that once a thread is started, you cannot repeatedly call the thread object's Start method, and invoking the Start method of the started thread will report the illegalstateexception exception, while you can repeatedly invoke the Run method


Here is a demo of the Start method and the Run method
The task in the thread is the name of the current thread that the string value passed in by the print thread
You can clearly see the difference between the two.


public class Diffbewteenstartandrun {public


	static void Main (String args[]) {


		System.out.println ( Thread.CurrentThread (). GetName ());
		Creating two threads for start and run method call
		Thread startthread = new Thread (New Task ("Start");
		Thread runthread = new Thread (New Task ("Run"));


		Startthread.start ();  Calling start method to Thread-will execute in
								//new Thread
		runthread.run ();//Calling Run method of thread -Would execute in
							//current Thread


	}


	/
	 * Simple Runnable implementation * *
	private static Class Task implements Runnable {
		private String caller;


		Public Task (String caller) {
			This.caller = caller;
		}


		@Override public
		Void Run () {
			System.out.println ("Caller: + Caller
					+" and "code") D By: "
					+ Thread.CurrentThread (). GetName ());}}}



Read MORE:HTTP://JAVAREVISITED.BLOGSPOT.COM/2012/03/DIFFERENCE-BETWEEN-START-AND-RUN-METHOD.HTML#IXZZ3DNSHNTSS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.