Java Basic Learning Summary-----Thread (i)

Source: Internet
Author: User

go: Basic Learning Summary----thread (i) Http://www.cnblogs.com/xdp-gacl/p/3633936.htmljava

First, the concept of threading

thread : A thread is a different execution path within a program

process : A static concept, A. class file on a machine, an. exe file on the machine, which is called a process.

program execution Process :

1. First put the code of the program into the code area of the memory, the code is placed in the code area and did not immediately start execution, but this time shows that a process is ready to start, the process has been generated, but has not started to execute, this is the process. It's actually 2. The execution of a process refers to the process in which the main thread (that is, the main () method) begins to execute.

3. A process is a static concept in which the threads are actually running in our machine.

4. In fact, at a point in time, the CPU has only one thread running, because the CPU is running fast, so we look like a multi-threaded.

Second, thread creation and start-up

1. In Java, Java threads are implemented by the Java.lang.Thread class, and each thread object represents a new thread.

2. You just have to new a thread object, and a fresh one will appear.

3. each thread completes its operation by means of the method run ( ) that corresponds to a particular thread object, and the method run () is called the thread body.

 Public classTestThread1 { Public Static voidMain (String args[]) {//new A thread class object R1,r1 to invoke the run () method to startRunner1 R1 =NewRunner1 (); //to start a new thread, T has to new a thread object.//This is the thread (Runnable target) construction method used here.Thread T =NewThread (R1); //The start thread t,t thread executes the run () method, which is executed in parallel with the main thread R1T.start ();  for(inti = 0; I < 10; i++) {System.out.println ("Maintheod:" +i); }    }}//defining a class to implement the Runnable interface, implementing the Runnable interface means that the class is a thread classclassRunner1Implementsrunnable{ Public voidrun () { for(inti = 0; I <10; i++) {System.out.println ("Runner1:" +i); }    }}

public class TestThread1 {public static void main (String args[]) {//new A thread class object R1,r1 to invoke the run () method to start Runner1 r1 = new Runner1 ( );//To start a new thread T must be new a thread//is used here is the thread (Runnable target) construction method Thread T = new thread (r1);//start thread t,t thread executes the run () method, It is executed in parallel with the main thread R1 T.start (); for (int i = 0; i <; i++) {System.out.println ("Maintheod:" + i);}}} Defining a class to implement the Runnable interface, implementing the Runnable interface means that the class is a threading class Runner1 implements Runnable{public void Run () {for (int i = 0; i < 10; i++) {System.out.println ("Runner1:" + i);}}}

  

Java Basic Learning Summary-----Thread (i)

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.