Java review--Multithreading basics

Source: Internet
Author: User
Tags deprecated

1. Basic Concepts

1) Process: Running procedures, procedures are static concept, process is the concept of dynamic, process and process between the non-transport impact

2) Thread: Refers to the program in a separate sequence of flow control, thread attached to the process, he is the smallest execution unit! A task one thread.

3) Multithreading: Refers to a single program can run multiple different threads at the same time, perform different tasks. (It is necessary to interpret threads as serving different tasks)

4) Relationship: There can be one or more threads in a process, but at least one thread.


2. Role and Relationship

1) Multithreading Purpose: Maximize CPU Resource utilization

2) Main thread: The Java program starts a thread by default, which is the main threads. Also be sure to note that main is just a thread

is not a process, so the main exit does not represent a process exit, and all threads must stop if they want the process to exit.

3) The internal data and state of the process are completely independent, but multi-threading is the data of the common registers, so the multithreading will affect each other, but the thread switching is less than the process switching resource.

4) The internal data and state of multiple processes are completely independent, and multithreading is a set of system resources that share a piece of memory space, which can affect each other.

5) The data of the thread itself is usually only the register data, and the stack used by the program when it executes, so that the thread's switchover is less than the switching burden of the process.


3. Java Thread Definition

1) inherit the thread class, override the Run method, and at startup, a new instance invokes the instance's Start method

2) Implement the Runnable interface and implement the Run method; new instance of the current class is called start as a parameter of a thread instance at the same time

3) Each thread will have a name, which is the identity of the thread, but the identity is not unique, that is, multiple threads may have the same name.

4) Thread execution code is written in the Run method, and then start the thread through the Start method, and then the system defaults to call the Run method, the role of start is to prepare resources for the execution of the thread, if you call the Run method manually, then this time the thread started is not a real thread. He is an ordinary method, no longer running in the thread concept.

5) The Start method can only be called once, and after the thread has run out, it cannot re-invoke start, but must re-invoke start on a new instance of the thread, and multiple calls to start will throw a thread state exception (run is an exception).

6) for a single core multicore only determines the number of parallel threads, multi-threaded if only in the single core, in fact, the order of their operation is not controlled, people feel that multiple threads at the same time (macro parallel), and in fact, because multithreading is the CPU resource competition, so long as a certain thread competition to the resources can run, Multiple threads compete against each other, causing multiple tasks to cross over, so it feels like multiple threads are executing at the same time! A single-core processor can also run multiple threads, but these threads are actually serial (micro-serial) rather than parallel, but their order of execution is indeed indeterminate, because only one thread can be processed by the same time single-core! But unlike multicore, he is a real micro-parallel, and at the same time there can be multiple threads executing!


4, the relationship between Thread and Runnable

1) The thread class implements the Runnable interface, implements the Run method, the concrete implementation is the null judgment of his member variable runnable variable, if it is not empty, call its run method, so we write a class that inherits the thread must re-run method.

2) runnable is an interface, thread is a class, Java single-inheritance features determine runnable easier to extend

3) A runnable variable can be shared by multiple thread instances, which is important for thread resource sharing


5. Thread Pause, stop

0) Stop the thread using the Stop method, which is an unsafe method (the core is the way to throw an exception) and is also a depcrecated method

1) Flag method to stop the thread, through an identification amount to the loop out, the identification amount can be in the loop outside the assignment, can also be in the loop to change the value (this time can be directly break, return);

2) The use of interrupt to stop the thread, this is not recommended, the core is thrown out of the way, just because the interrupt thread in the blocking state is called when the interrupted value of the threads will be reset, resulting in inaccurate values, it is also not recommended!

3) suspend the thread through the sleep and yield methods, this is recommended, they will not release the object's lock!

4) It is not recommended to suspend wake-up threads through suspend and resume, because using these two methods can cause some unexpected things to happen, so the two methods are identified as deprecated (deprecated) tokens. This means that in future JDK versions, these two methods may be removed!


6. Thread Life cycle

1) Create: After new one instance

2) Operational (ready): Called Start, which is the ready state

3) Running status: Thread gets to CPU resource, processing

4) not operational (suspend, block): blocked, such as waiting for IO, waiting for object lock, etc.

5) Extinction: Thread runs out, or throws an exception

Note that the blocking state can be divided into the following situations:

A, blocking state in the object waiting pool: When the thread is running, if the Wait () method of an object is executed, the Java virtual machine puts the thread back into the waiting pool of the object.

B, blocking state in object lock: When the thread is running, attempting to acquire a synchronization lock on an object, if the synchronization lock of the object is already occupied by another thread, the JVM will place the thread in the pool of this object.

C, other blocking states: The current thread executes the sleep () method, or calls the join () method of another thread, or when an I/O request is made, it enters this state.

JDK Source Thread Status:

A thread state. A thread can is in one of the following states:

    • NEW
      A thread that has not yet started are in this state.

    • RUNNABLE
      A thread executing in the Java virtual machine was in this state.

    • BLOCKED
      A thread that's blocked waiting for a monitor lock are in this state.

    • WAITING
      A thread that's waiting indefinitely for another the thread to perform a particular action was in the this state.

    • TIMED_WAITING
      A thread that's waiting for another the thread to perform an action for up to a specified waiting time was in this state.

    • TERMINATED
      A thread that has exited is in the this state.

A thread can is in the only one state at a given point in time. These states is virtual machine States which don't reflect any operating system thread states.


7. Priority of Threads

1) If there is a priority qualification that could cause a low priority thread to never have a chance to execute, this is unreasonable, so the priority of the thread is not the only one that determines the execution of the thread! The operating system dynamically controls the priority of the thread when the program is running, for example, the longer the thread waits, the higher his priority will be to ensure that all threads have a chance to execute! So programmers should not rely too much on priority to determine thread execution!

2) priority is set to facilitate system-to-thread scheduling in multi-threaded environments, high priority threads will take precedence, but it is not recommended to use precedence to control the order in which threads are executed;

3) When a thread is created, the child inherits the parent's priority

4) After thread initialization, change the priority by calling the SetPriority method

5) The priority of a thread is a positive integer between 1~10


8. Scheduling of Threads

The thread scheduler chooses the highest-priority thread to run. However, the running of the thread is terminated if the following conditions occur:

A: The yield method is called in the thread body, freeing up the use of CPU resources

B: The Sleep method is called in the thread body, either the thread is dormant or it may give up the use of CPU resources

C: The thread is blocked due to I/O operations, and other blocking states cause the CPU resources that were originally occupied to be freed

D: In a system that supports time slices, the time slice of the thread runs out


Java review--Multithreading basics

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.