Java multithreaded Programming (a basic concept)

Source: Internet
Author: User

1. Processes and Threads

process, is a running program entity, Xxx.exe is common under Windows, and many processes can be seen in Task Manager. It is a container for threads.

Thread, which is an execution flow in the process. In single-threaded programming, our program has only one execution flow: The main method of the main thread. Flow, indicating that the execution of the process is sequential, such as the main function of the statement requires a one-line execution, the first statement is not finished, you cannot execute the second statement.

Visible, single-threaded programming is limited, that is, our statements can only be executed serially, it is not possible to happen some statements simultaneously execution phenomenon. Sometimes we want some code to execute in parallel, like we're eating while watching TV. This time, you need to use multithreaded programming technology.

It is important to note that a program has at least one thread, and that is the main path, which corresponds to the main method. Then, the creation of other threads requires the creation and initiation of a thread that has previously existed. This pre-existing thread can be the main method of the main thread, or it can be a new thread created by main.

For example, in the main thread, run to a moment, create (as for how to create a new thread will later explain) the new thread thread-1, since then, the CPU is more "busy" than before, because it is going to execute 2 threads total code. Is it really "executing" the Code of 2 threads at the same time? The answer is in the negative. The CPU itself is not the so-called parallel execution ability, also does not matter multi-threading, multithreading is the native operating system support. the operating system manages the process threads, and in turn (without a fixed order) allocates a short period of time (not necessarily evenly distributed) for each process. then inside each thread, the program code handles the time allocation of the internal threads of the process itself, and the switching time between the threads is very short. So multitasking, multi-process, multi-threading is a kind of macroscopic feeling of the operating system, from the microscopic point of view, the operation of the program is executed asynchronously.

We know it, and we don't need to be concerned about what the underlying CPU does. Our concern is how to create and manipulate our threads through code. Let's just make our threads "executed in parallel".

2. Call stack

What is the call stack? In previous single-threaded programming, take the main function. With a Lezilai description.

 Public class Test  {        publicstaticint Add (int x,int  y)    {         return x+y;    }      Public Static void Main (string[] args)     {                int x =2,y = 5;         int re = Add (x, y);                System.out.println (re);            }    }

The operating system calls the main function, because the main function is the entry of the program, then the main function is in the stack, main will occupy a certain amount of memory, because there are parameters args and local variable x,y,re. When executing to the Add function call, this time the main function "hang", the execution flow into the Add function, the Add function into the stack, the same system will also allocate temporary memory space for the Add function, when the Add function is finished, the Add function out of the stack, return the result, it occupies the memory is recycled, The execution flow goes back to the main function again, runs to the println function, println the function into the stack ..... Println out of the stack, the execution flow back to Main,main execution, the program ends, the system reclaims the memory of this program.

Note: The stack here is just a model of the operating system management function call, not the data structure of the stack, only enough of the two logical structure is consistent: FILO.

In multithreaded programming, each thread has its own call stack to manage its own function calls.

3. Things to Remember

One, the sharing of memory data between threads, which makes data access more fast and convenient.

Second uses multithreaded programs, and if any one thread does not end, then the program will not end up running.

Third, the JVM's thread scheduling mode adopts preemptive mode. Preemptive scheduling is based on the priority level of the thread to obtain the use of the CPU. But this is not necessarily the order in which the CPU threads are executed by the operating system and the JVM

decision. But the single execution time of each thread is certain, this time is called the time slice, in the Linux system, the default time slice is 1/100s.

Java multithreaded Programming (a basic concept)

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.