Java Thread Cheng

Source: Internet
Author: User

/*
At some point, the CPU can only execute a thread in a process (with the exception of multicore, multi-core, multiple CPUs). Why do you feel that watching TV and playing games can be done at the same time? This is because the CPU is constantly switching between applications quickly, and you won't feel it at all.
And when there are multiple threads in a process, the CPU is switching between threads, which speeds up the execution of the entire program, giving the impression that threads are executing synchronously.

What we can feel in real life is that the more applications the computer opens, the more slowly it feels, that is, the computer is stuck, because the CPU is too busy and the processing is relatively slow.

Process: is an executing procedure;
Each process execution has an execution order. The order is an execution path, or a control unit.
The process is to allocate memory space for the execution of the application, and the thread will be encapsulated.

Thread: is an independent control unit in the process. The thread is controlling the execution of the process. In fact, the program actually executes a thread when it runs.

There is at least one thread in the process.

There will be a process when the JVM starts Java.exe

At least one thread in the process is responsible for the execution of the Java program. And the code in this thread is stored in the main method. This thread is called the primary thread.

Extension: In fact, more details indicate that JVM,JVM started more than one program. There are also threads responsible for the garbage collection mechanism.
*/
/*
1, how do you customize a thread in your custom code?
By looking at the API, Java has provided a description of what the thread is like. is the thread class.
The first way to create a thread: Inherit the Thread class.
Steps:
1, defines the class that inherits the thread class.
2. The Run method in the thread class is replicated.
Purpose: Store your custom code in the Run method. Let the thread run. The code executed in the thread is placed in the Run method.
3, call the thread's Start method, which has two functions: start the thread and call the Run method.

*/

/*
Describes the methods commonly used in several threads:
1,static thread CurrentThread (): Gets the current thread object
2,getname (): Gets the thread name

Set thread Name: SetName or constructor

*/
Class Demo extends Thread{//private string Name;demo (string name) {//this.name=name;super (name);//rename thread}public void Run () {for (int i=0;i<200; i++) {System.out.println (Thread.CurrentThread (). GetName () + "Run");//getname gets the name of the Thread object}} }class threaddemo{public static void Main (String args[]) {Demo D1 = new Demo ("Demo1");D emo d2 = new Demo ("Demo2");d 1.start ();//Open the thread and execute the thread's Run method. D2.start ();//d.run (); If you call the Run method directly without calling the Start method, the thread does not open, only the object method is called. This thread is opened only if start is called. for (int j=0;j<200; j + +) {System.out.println ("Hello world!");}}}

Java Thread Cheng

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.