Can the run() method be called directly to start a thread?
What is a thread? A thread is a single sequential flow of control within a program. The thread behaviors include starting, sleeping, running, yielding, and having a priority. There are a number of situations where you might want to explicitly use threads to improve the performance, responsiveness, or organization of your programs. These include:
- Making the user interface more responsive when performing long tasks
- Exploiting multiprocessor systems to handle multiple tasks in parallel
- Simplifying modeling of simulations or agent-based systems
- Performing asynchronous or background processing
There are lots of interesting thread related subjects to discuss. It's not trivial to write thread-safe program. If you don't pay close attention, you will encounter problems like deadlock, race condition, and starvation in detail, etc. The discussion on writing applications to use thread are endless. For more information, refer to SCJP Study Guide: Thread
Here we focus on what are the proper ways to start a thread.
Let's review how a thread can be created in a Java application. There are two ways to create a thread. One is to subclass Thread and override the run() method; The other one is to implement the Runnable interface.
Subclassing Thread and overriding run() Method
The following is an example of creating a thread by subclassing the Thread class:
import java.lang.*;
public class MyThread extends Thread
{
public MyThread (int count) {
this.count = count;
}
private int count = 0;
public void run()
{
.... /*put all the code here that you want this
thread to accomplish */
}
}
The run() method is where the meat is, it contains the code to implement the logic that this thread class is designed to do.
The proper way to start this kind of threads is to call the start() method of the class:
MyThread t = new MyThread(5);
t.start();
Implementing the Runnable interface
The following is an example of creating a thread by implementing the Runnable interface:
class MyRunnable implements Runnable
{
public MyThread (int count) {
this.count = count;
}
private int count = 0;
public void run()
{
.... /*put all the code here that you want this
thread to accomplish */
}
}
The proper way to start this kind of threads is to create an instance of the Runnable class, and pass the reference to the Thread constructor, then call the start() method of the Thread:
MyRunnable r = new MyRunnable(5);
new Thread(r).start();
Why it's only proper to call the start() method to start the thread instead of calling the run() method directly?
In both the above examples, the run() method is the most important method in the thread classes, it is also the only method that we need to implement in both cases. Why it's only proper to call the start() method to start the thread instead of calling the run() method directly? Because the run() method is not a regular class method. It should only be called by the JVM. Writing thread classes is not about a single sequential thread, it's about the use of multiple threads running at the same time and performing different tasks in a single program. The JVM needs to work closely with the underneath operating system for the actual implementation of concurrent operations. This is how the performance can be improved, and all other benefits mentioned above can be achieved.
You should not invoke the run() method directly. If you call the run() method directly, it will simply execute in the caller's thread instead of as its own thread. Instead, you need to call the start() method, which schedules the thread with the JVM. The JVM will call the corresponding run() method when the resources and CPU is ready. The JVM is not guaranteed to call the run() method right way when the start() method is called, or in the order that the start() methods are called. Especially for the computers have a single processor, it is impossible to run all running threads at the same time. The JVM must implement a scheduling scheme that shares the processor among all running threads. This is why when you call the start() methods from more than one thread, the sequence of execution of the corresponding run() methods is random, controlled only by the JVM.
Most Recent java Faqs
- How to uncompress a file in the gzip format?
- How to make a gzip file in Java?
- How to use Java String.split method to split a string by dot?
- How to validate URL in Java?
- How to schedule a job in Java?
- How to return the content in the correct encoding from a servlet?
- What is the difference between JDK and JRE?
Most Viewed java Faqs
- How to read input from console (keyboard) in Java?
- How to use HttpURLConnection POST data to web server?
- How to add BASIC Authentication into HttpURLConnection?
- How to Retrieve Multiple Result Sets from a Stored Procedure in JDBC?
- What are class variables in Java?
- What are local variables in Java?
- How to Use Updatable ResultSet in JDBC?