Java (9) ------- thread (1)

Source: Internet
Author: User

Package com. gc. thread;/*** process: a program in progress. each process execution has an execution sequence, which is an * execution path, or a control unit * thread: an independent control unit in the process, A thread controls the execution of a process ** at least one thread in a process * When the java vm starts, there will be a process java.exe * at least one thread in the process, responsible for java program execution, and the code run by this thread exists in * main method * this thread is called the main thread ** extension: in fact, more details about jvm, jvm starts more than one thread, and there is also a thread responsible for the garbage collection mechanism ** 1. how can I customize a thread in custom code? ** by searching APIs, java has provided descriptions of such things as threads, for the Thread class '* The first way to create a Thread: Inherit the Thread class * Step: * 1. define class inheritance Thread * 2. re-write the run method in the Thread class * objective: to store the defined code in the run method and run the Thread * 3. call the start method of the thread. This method has two functions: * start the thread and call the run method **. It is found that the running result is different every time, because multiple threads obtain the execution right of the cpu. when the cpu is executed * to whom, it is clear that at a certain time point, only one program is running (except for multiple cores) * The cpu is switching quickly, to achieve the effect that seems to be running at the same time. * We can image the multi-thread running behavior to snatch the cpu execution right. * This is a multi-thread feature: randomness * who gets the execution right, and how long the execution will take, the cpu calculates. * ** why overwrite the run method ** the Thread class is used to describe the Thread * class defines a function to store the code to be run by the Thread. this storage function is the run method ***, that is, the run method in the Thread class, used to store the code to be run by the Thread */class Demo extends Thread {public void run () {for (int x = 0; x <60; x ++) System. out. println ("demo run") ;}} public class ThreadDemo {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubDemo d = new Demo (); // create a thread d. run (); // only the method called by the object, but the thread is created and not run. // d. start (); // start the thread and execute the run method for (int x = 0; x <60; x ++) {System. out. println ("hello world ");}}}

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.