Q: A buddy's multi-thread questions about start () and run ()

Source: Internet
Author: User
The reason why a thread occurs is to make better use of the CPU and make her work more "savvy.

Start a thread by calling the START () method of the thread class. The thread is in the ready state and is not running.

This thread class then calls the method run () to complete its operation. Here the method run () is called the thread body, which contains the content of the thread to be executed, the run method ends and the thread ends, and the CPU then runs other threads,

If you use the run method directly, this is just a method call,ProgramThere is still only the main thread-this thread, and there is only one program execution path, so it does not reach the purpose of writing the thread.

The thread is used to make better use of the CPU and increase the program running rate!

 

Source code:

Start is the thread entry through which the thread is started

Public synchronized void start () {If (started) throw new illegalthreadstateexception (); started = true; group. Add (this); start0 ();}

Run is the actualCodeExecution segment. If run is called directly in the program, it becomes a common method, because it is not added to the thread group. If run is used, the program will be suspended, because it is just a call to a conventional method, it still uses the main thread.

 

For example:

The concept of Multithreading is not much to be said here. For example, you can think of yourself as a process and everything you do as a thread. You can play Warcraft and listen to songs at the same time, playing Warcraft and listening to songs are two threads, multi-thread.
Java is one of the few languages that support multiple threads in a centralized manner. Most languages can run only one program block and cannot run different program blocks at the same time. Java makes up for this defect.
A simple example of a company's project development is that after a user uploads a compressed file to the server, he needs to perform two operations on the compressed package. One is to copy the compressed package to the specified directory, first, extract the package to another specified directory, and finally respond to the user's prompt that the file is uploaded successfully. If the compressed package is large, the copy and decompression function after upload takes a long time, and the user will wait for a long time. In fact, the copy and decompression functions are not directly related to user operations and can be completely independent. The solution is as follows:

After the user uploads the compressed file, we immediately create two threads: one is the thread that copies the compressed file, and the other is the thread that decompress the compressed file. We can pass the file information to the corresponding thread through the thread constructor. When we start the start method of the two threads, we no longer need to care about the replication and decompression operations, instead, it directly responds to the user, so that the user obviously feels that the operation is faster, and the operation of copying and decompression is still carried out secretly in the background.

There are two methods to implement multithreading: one is to inherit the thread and the other is to implement the interface runnable. There is not much difference between the two. inheritance can only be a single inheritance, but multiple interfaces can be implemented, so I prefer to use the latter.

The following code model is provided for your reference:

Package COM. bankht. test; import Java. io. file;/*** @ Author: -AK47 * @ Creation Time: 09:49:56 ** @ Class description: multi-thread instance details. */public class fileoperate {public static void main (string [] ARGs) {long begin = system. currenttimemillis (); // Upload File uploadfile = new uploadfile (); file = uploadfile. uploadfilemethod (); // pass the coppyfile = new coppyfile (File) to the thread; unzipfile = new unzipfile (File ); // create thread coppythread = new thread (coppyfile); thread unzipthread = new thread (unzipfile); // start thread coppythread. start (); unzipthread. start (); long end = system. currenttimemillis (); // responds to the user request system. out. println ("congratulations, the file has been uploaded successfully. Time consumed:" + (end-begin) + "millisecond ");}} /*** Upload File class */class uploadfile {// File Upload public file uploadfilemethod () {file = new file ("filepath"); system. out. println ("File Uploaded"); Return file ;}/ *** copy file class */class coppyfile implements runnable {private file; Public coppyfile (File file) {This. file = file ;}@ overridepublic void run () {coppyfilemethod (File);} // copy the public void coppyfilemethod (File file) {long begin = system. currenttimemillis (); // sleep for 15 seconds try {thread. sleep (5*1000);} catch (interruptedexception e) {e. printstacktrace ();} long end = system. currenttimemillis (); // responds to the user request system. out. println ("File Replication completed, time consumed:" + (end-begin) + "millisecond ");}} /*** decompress the file class */class unzipfile implements runnable {private file; Public unzipfile (File file) {This. file = file ;}@ overridepublic void run () {unzipfilemethod (File);} // decompress the public void unzipfilemethod (File file) {long begin = system. currenttimemillis (); // sleep for 10 seconds try {thread. sleep (3*1000);} catch (interruptedexception e) {e. printstacktrace ();} long end = system. currenttimemillis (); // responds to the user request system. out. println ("extract part of the file, time consumed:" + (end-begin) + "millisecond"); try {thread. sleep (3*1000);} catch (interruptedexception e) {e. printstacktrace ();} Long end1 = system. currenttimemillis (); system. out. println ("decompress the file, time consumed:" + (end1-begin) + "millisecond ");}}

Run:

 
Congratulations, the file has been uploaded. It takes 15 milliseconds to decompress a part of the file. the time consumed is 3000 milliseconds after the file is copied. the time consumed is 5000 milliseconds after the file is decompressed. the time consumed is 6000 milliseconds.

 

 

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.