Thread of Java

Source: Internet
Author: User

Thread of JavaOne: Thread:

What is a thread? Let's take a look at the official explanation that threads, sometimes referred to as lightweight processes, are the smallest unit of program execution flow. A standard thread consists of a thread ID, a current instruction pointer (PC), a collection of registers, and a stack. In addition, the thread is an entity in the process, is the basic unit that is dispatched and dispatched by the system independently, the thread does not own the system resources, only has a bit of resources that are essential in the operation, but it can share all the resources owned by the process with other threads belonging to one process. One thread can create and revoke another thread, which can be executed concurrently between multiple threads in the same process. Each program has at least one thread, and if the program has only one thread, it is the program itself.

A thread is a single sequential control flow in a program. A relatively independent, scheduled execution unit within a process is the unit of dispatch for the system to dispatch and dispatch CPUs independently of the operating program. Running multiple threads at the same time in a single program accomplishes different tasks, called multithreading.

I think threads are likebossthe subordinate, the thread of thebossis thatThe main function (which can also be said to be our program),bossassigned a task to subordinates, subordinates to complete their own,bosswill not be ignored, of course, our threads are very strong, the task will be consciously completed. And our thread subordinates can also go to own thread subordinates, and the thread subordinates are not interfering with each other, do their own things, so their order is no way to complete control. A thread subordinate can have no resources, that is, only the resources that must be required to perform the task, but the thread subordinates can be shared with other thread subordinatesbosshave all the resources, after all, everyone is a gang. and multithreading isbossThere are multiple subordinates, the other thread is a separate running unit of the process, it can be said that the process is composed of threads, can be a thread, can be multiple threads, and the process can be in the running program, and the program only one thread, then the thread isboss,bossIs also the program;

And then we take a look at the process, the process can be a running program, the work of Boss,boss all to their own place of work, the process is running in memory, the thread is running in the process, the thread will not have its own independent memory space, the thread after running, unable to get other threads to change objects, properties or variables, That is, thread quality inspection, after the start of work, it is not pleasant to communicate. And with the variable before the volatile can be solved.

Second: The implementation of the thread

1. Two ways to implement threading:

One: Inherit the tread class;

Run () operation method;
Start () method to start the thread;
Sleep (int time) thread sleep method, parameter is the length of sleep;

Two: Implement Runnable interface

Only the run () running method of the thread is present;

2. The benefits of implementing the Runnable interface than inheriting the thread class:

1): Suitable for multiple threads of the same program code to process the same resource

2): Can avoid the restriction of single inheritance in Java

3): Increase the robustness of the program, the code can be shared by multiple threads, code and data Independent.

Thread of Java

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.