"Java multi-threaded programming practical guide (core papers)" Reading Notes

《Java多线程编程实战指南(核心篇)》阅读笔记

"Java multi-threaded programming practical guide (core papers)" Reading Notes

1 multi-threading concepts

1.1 Processes, Threads

process
The basic unit of application program resources to the operating system (such as memory and file handles) of
Thread
The smallest unit that can be performed independently of the process

A process can contain multiple threads share the process resources with all threads in a process, such as memory space, file handles and so on.

1.2 the Java thread creation

In the Java platform to create a thread is to create an instance of the Thread class (or subclass); a running thread actually make Java virtual machine executes its run method, the corresponding task processing logic code execution, start Thread class method of action is to start the appropriate thread.

start method call that does not mean the end of the corresponding thread already running, this thread might run was only later, and even may never be run. Because the essence of a thread start is to request the corresponding Java virtual machine running thread, and this thread is exactly when to run is determined by the thread scheduler (Scheduler) is.

Creating Java threads
Thread two classes of common constructions are: Thread and Thread (Runnable target), Thread abstract thread, Runnable may be seen as an abstraction of the task.
new new  the Thread () {
     @Override 
    public  void  RUN () {
         // perform tasks 
    } 
}; new new the Thread ( new new the Runnable () {
     @Override public void RUN () {
         // perform tasks 
    } 
});
  
      
The thread can not be reused
Threads are "one-time items," we can not re-start method is called the end of an already running thread to make it run again. In fact, multiple calls to start the same way as a Thread instance can cause it to throw IllegalThreadStateException exception.
Memory allocation
First, in Java, a thread is an object, allocate memory space required to create the object. And create other types of Java objects The difference is, Java virtual machine call stack (Call Stack) required memory space allocated for each thread. Call stack for call tracking methods, and the relationship between the Java code calls to native code (Native Code, usually C code). In addition, Java platform, each thread there may be a kernel thread (specific and implementation of the Java virtual machine-related) corresponding. So relatively speaking, to create a thread object is higher than other types of objects to create a number of costs.
Thread of execution and the current thread
Java in any piece of code (such as a method) is always responsible for determining the execution thread, the thread will accordingly be called threads of execution of this code; any piece of code can be obtained by calling Thread.currentThread () thread of execution of this code, this thread is called the current thread.

1.3 thread (Thread) properties

Properties include thread thread number (Id), Name (Name), thread category (Daemon) and priority (Priority).

  Attributes      Types of                  use                Read-only                                 Explanation                                
Number (ID)  
          
          
          
          
long   
       
       
       
       
Used to identify different threads, different threads have
different numbers                        
                                  
                                  
                                  
Yes   
     
     
     
     
After a certain number of threads running, the number of threads may be created subsequent use. Different thread 
owns numbers are different, but the uniqueness of this number only one run in the Java virtual machine 
efficiency. That restart a Java virtual machine (such as restarting the Web server), some threads may be numbered
the same as the last number of a thread running Java virtual machine, so the value of this property is not suitable for 
some kind of unique identifier , especially as the unique identifier in the database (e.g., primary key)                 
Name      
(Name)    
          
String 
       
       
For distinguishing between different threads, the default value of the thread
ID related format default values is: "Thread- 
thread number", such as "Thread-0"           
no   
     
     
Java does not prohibit us the name of the property to a different thread of the same value, however, set 
set thread name and code debugging properties can help locate the problem                           
                                                                   
Thread category  
(Daemon)  
          
          
boolean
       
       
       
Tnue value indicates that the corresponding thread as a daemon thread,
otherwise it means the corresponding thread is a user thread. The genus
default value of the parent metal with the corresponding thread of the thread
values of the same                        
no   
     
     
     
The property must be set before the corresponding thread is started, that is a call to setDaemon method must be on  
before the start method is called, otherwise setDaemon method throws                       
IllegalThreadStateException exception. Responsible for a number of mission-critical processing threads inappropriate setting
is set to daemon thread                                                       
Optimization level    
(Priority)
          
          
          
          
          
int    
       
       
       
       
       
       
Is presented to the thread scheduler on the nature of the property,
it is used to indicate an application which thread can expect excellent
first be run. Java defines 10 to 10, preferably 1
priority, the default value is generally 5 (showing the normal priority
). For a particular thread, its priority 
level of defaults and parent thread (the thread create 
thread) priority values are equal.             
no   
     
     
     
     
     
     
Generally use the default priority can not properly set the property value may cause serious problems (thread
starvation)                                                              
                                                                   
                                                                   
                                                                   
                                                                   
                                                                   
Using threads property
In addition to the properties of the thread number, the other are readable and writable attribute properties, i.e., the Thread class provides the get and set methods for reading or setting the appropriate properties.
Thread.currentThread().getName();
Priority setting
The nature of Java thread priority attribute of just a message to the thread scheduler to decide what priority scheduling threads running thread scheduler. It does not guarantee that threads run in the order of their priority level. In addition, Java thread priority misuse or abuse may cause some threads never get to run, which produce a thread hungry money (Thread Starvation). Therefore, the priority of the thread is not set too high as possible; you can use normal priority under normal circumstances, that is not necessary to set the priority attribute thread.
User threads and daemon threads
Depending on whether Java virtual machine thread prevents normal stop, we can in Java thread into a daemon thread (Daemon Thread) and user thread (User Thread, also known as non-daemon threads). Which prevents the normal user threads to stop the Java Virtual Machine, a Java virtual machine that is stopped only in the case of all of its normal user threads run ended; and daemon threads will not affect the normal stopping the Java virtual machine, namely the application there daemon thread running does not affect the normal stop virtual machines. Therefore, the daemon thread is usually used to perform some of the tasks is not very high importance, for example, for monitoring the operation of the other threads.

Date: 03/09/2019 Your

Author: C.Wong

Created: 2019-09-04 三 00:56

Validate

Guess you like

Origin www.cnblogs.com/channingwong/p/11456634.html