A three   Hospital Medical    test tube  substituting  pregnancy D

 Micro Signal █ █: 138-0226-9370█ █ █ surrogate packet success packet healthy surrogate ████████████ three  A  Hospital Medical    test tube  substituting  pregnancy D

First, the process

  Process is the underlying operating system structure; is a program execution; active that occurs when a program on the order of execution and data processor. Operating system, almost all of the corresponding task running a process (Process). A program into the memory to run, that becomes a process. Process is in the process of running the program, and with a certain separate functions. Description of the process there is a word very classic - a process system is a separate unit of resource allocation and scheduling.

  System processes are independently existing entity, with its own independent resources, it has its own private address space . The essence of the process, the program is executed in a multiprogramming system, a process that is dynamically generated, dynamic extinction, has its own life cycle and various states. Processes with concurrency, it can be executed concurrently together with other processes, according to the independent, unpredictable pace to move forward. 

(Note that, concurrent (Concurrency) and parallelism (Parallel) are different. Refer to the same time in parallel, a plurality of instructions to run simultaneously on multiple processors concurrently referring to the same time, only one instruction is executed, but more than one process is quick rotation instruction is executed, it looks as if the multiple instructions simultaneously execute the same.)

  Process by the program , data , and control block process consists of three parts.

  

Second, thread

  Thread, sometimes referred to as lightweight processes (Lightweight Process, LWP), is the smallest unit of program execution flow. A standard thread by the thread ID, the current instruction pointer (PC), register set and stack components. In addition, the thread is a physical process, is the basic unit of independent scheduling and dispatch system, the thread does not own its own system resources, has only a little in the operation of essential resources, but with the other it may belong to the same process threads share the process have all the resources . A thread can create and undo another thread can execute concurrently across multiple threads in the same process. Due to the interaction between threads, resulting in the thread showing a discontinuity in the operation. Each program has at least one thread, if the program has only one thread, and that is the program itself.

  A thread is a single sequential program control flow. Simultaneously run multiple threads to complete different jobs in a single program, called multi-threading.

    In Java Web should pay attention to thread the JVM level, without stopping, with the demise of the common JVM, that is, if a Web service launched multiple Web applications, a Web application started a thread, if you turn off this Web application, the thread does not close, because the JVM is still running, so do not forget to stop the thread when setting up Web application is closed.

 

 Third, the thread state   

    Thread package includes the following five states.

1.  New state (New)          : After the thread object is created, entered the new state. At this point it and other Java objects, only the memory allocated by the Java Virtual Machine, and initializes variable values of its members.

2.  ready state (Runnable) : also known as "executable state." Thread object is called the object's start () method, the thread is in the ready state. Java virtual machine call stack and program counter to create a method. Thread in the ready state, it could be executed at any time CPU scheduling, depending on the scheduling JVM thread scheduler.

3.  Run state (Running)  : CPU thread to obtain permission for execution. Note that, the thread can only be entered from the ready state to the running state.

4.  blocked (Blocked)   : the thread is blocked for some reason to give up the right to use CPU temporarily stops running. Until the thread into the ready state, a chance to go running. Case of obstruction of three categories:
    (01) blocking wait - wait by the calling thread () method, thread to wait for the completion of a job.
    (02) synchronous blocking - a thread get synchronized synchronous lock failure (because the lock was occupied by another thread), it will enter synchronous blocking state.
    (03) other obstruction - through the sleep () or join the calling thread () or issue an I / O request, the thread into the blocked state. When sleep () timeout, the Join () or a timeout wait for a thread to terminate, or I / O processing is completed, the thread into the ready state again.

5.  death state (Dead)     : thread execution is over, due to the abnormal exit the run () method or by calling the thread's stop () method directly (easily lead to deadlock, is now not recommended), the thread end of the life cycle.

  

Four, wait (), notify (), nofityAll () method 

  In Object.java, the definition of the wait (), notify () and notifyAll () method and the like.

  Role wait () is to allow the current thread into a wait state, at the same time, wait () also makes the current thread releases the lock it holds .

  The role notify () and notifyAll (), it is a wake up waiting threads on the subject of the current; notify () is to awaken a single thread, notifyAll () is a wake-up all the threads.

API Class Object details regarding wait / wake follows:
  Notify () - wake up on a single thread waits monitor this object, it enters the "ready."  
  notifyAll () - wake up all threads waiting on this object's monitor to make it into the "ready state."
  wait () - allow the current thread in a "wait (blocking) state", "until another thread invokes the object's notify () method or the notifyAll () method", the current thread wakes up (into the "ready state").
  wait (long timeout) - Let the current thread in a "wait (blocking) state," "() method or the notifyAll () method, or more than a specified amount of time until another thread calls this object notify", the current thread wakes up ( into the "ready state").
  wait (long timeout, int nanos) - Let the current thread in a "wait (blocking) state", "until another thread invokes the object's notify () method or the notifyAll () method, or some other thread interrupts the current thread, or more than one of the actual amount of time ", the current thread wakes up (into the" ready state ").

 Role wait () is to "current thread" wait (lock is released), and the "current thread" is the correct thread running on cpu!

  

Five, yield (), sleep () , join () and interrupt () method   

1、yield() 

  yield () is a static method of the Thread class. It allows the current thread is suspended, but will not block the thread, but by "running state" into a "ready state", so that other waiting threads with the same priority access to executive power; however, no guarantee that the current thread after calling yield (), the other with the same priority thread will be able to get the implementation right; also possible that the current thread has entered into the "operating state" continues to run!

  It is noteworthy that, yield () method does not release the lock .

 

2、sleep()

  sleep () is a static method of the Thread class. The method declaration throws InterrupedException exception. So when using either catch or throw statement.

  There are two overloaded methods:

--static void sleep (long millis): the currently executing thread to pause millis milliseconds, and enters the blocked state, which affected the precision and accuracy of system timers and thread scheduler.

--static void sleep (long millis, int nanos): the currently executing thread to suspend millis milliseconds plus nanos microseconds, and enters the blocked state, which affected the precision and accuracy of system timers and thread scheduler.

  the role of sleep () is to allow the current thread to sleep, that is, from the current thread " running state into the" to " sleep (blocked) state ." sleep () specifies the sleep time, sleep time thread may be greater than / equal to the sleep time; re-thread wakes up, it will be a " blocked " to " ready ", so that the cpu waiting to be scheduled execution. Used to pause the program running.  

  Also note, SLEEP () method does not release the lock .

 

3、join()

  join () is an example of a method of Thread. He said that when a program execution method call join other threads in the stream, the calling thread will be blocked until join thread execution is completed.

There are three overloaded forms:

--join (): waiting to be join the thread execution is completed

--join (long millis): waiting to be the longest thread join is millis milliseconds, if in millis milliseconds, join the end has not been performed by a thread, without waiting.

--join (long millis, int nanos): waiting to be most millis milliseconds plus nanos microseconds, if at this time, the thread join the end time has not been performed the thread of the join, without waiting.

That is, after the current thread, with a thread object to call join (), will make the current thread wait until the thread to run the thread object is finished, the original thread will continue to run.

 

4、interrupt()   

  We often judge by the thread interrupt flag to control threads.   

  interrupt () is an instance of class Thread method for interrupting this thread. When this method is called, the thread will immediately interrupt flag is set to "true". So when the interrupt is "blocked" thread, as is blocked, interrupt flag will be set to "false", throw an InterruptedException. So we catch this exception outside the loop of thread, you can exit the thread.

  After the interrupt () does not interrupt in the "running" in the thread, the thread it will "interrupt flag" is set to true, so we can continue by isInterrupted () to detect interrupt flag, thus calling the interrupt () termination thread, which is usually we use to interrupt () is.

  Interrupted () is a static method of the Thread class, which returns a boolean indicating whether the current thread has been interrupted, isInterrupted () is an instance of the Thread class method that returns a boolean to determine whether the thread has been interrupted. They can be used to detect an object "interrupt flag." The difference is, interrupted () In addition to returning interrupt flag outside, it also clears the interrupt flag (about to interrupt flag is set to false); and isInterrupted () simply returns the interrupt flag.

  

Six, Synchronized keyword 

1, the principle

  In java, and each object has only a synchronization lock. This also means that synchronization lock is dependent on the object exists.

  When the current thread calls a synchronized method for an object, you get the synchronization lock of the object. For example, synchronized (obj), the current thread will get the "obj the object" genlock.

  Different threads to access synchronization locks are mutually exclusive. In other words, a certain point in time, synchronized lock object can only be acquired by a thread! By genlock, we will be able to multi-thread, to achieve mutually exclusive access to the "Object / Method". For example, now there is a thread A and thread B, which will be accessing the "sync lock object obj." Suppose, at some point, the acquired thread A "synchronization lock obj" and perform some operation; At this point, the thread B can attempt to obtain "the synchronization lock obj" - thread B will acquisition has failed, it must wait, a thread until the release of "sync lock of the object" after thread B to get to the "obj sync lock" so that it can run.

 

2, the basic rule

  Article: When a thread accessing "an object" in "synchronized method" or "synchronized code block", other threads of the "synchronized method" or access "to the object" a "synchronized code block" is to be blocked.
  Article: When a thread accessing "an object" in "synchronized method" or "synchronized code block", still other threads can access "to the object" asynchronous block .
  Third: When a thread accessing "an object" in "synchronized method" or "synchronized code block", other threads to access "to the object" other "synchronized method" or "synchronized code block" is to be blocked .

 

3, and the global lock locks Examples

Examples Lock  - a lock on an instance of the object. If the class is a single embodiment, it also has the concept of a global lock that locks.
               Examples of the corresponding lock is synchronized keyword.
Global lock  - the lock for that class, no matter how many object instance, then the threads share the lock.
               Global Lock is the corresponding static synchronized (or the lock on the class classloader class or object).

  That is to say, a non-synchronized keyword on the static method, the method relies on behalf of their respective objects. Synchronized keyword on a static method, the method relies on behalf of the class itself.

  

Seven, thread priorities and thread guard  

1, thread priority  

  Java thread priority range is 1 to 10, the default priority is five. Each thread default priority are creating its parent thread with the same priority. By default, mian thread has normal priority. "High priority thread" will give priority to "low priority thread" execution. Thread provides setPriority (int newPriority) and getPriority () methods to set and return the thread priority.

  Thread class has three static constant:

——MAX_PRIORITY = 10

——MIN_PRIORITY = 1

——NORM_PRIORITY = 5

 

2, daemon thread

  java There are two threads: the user thread and thread guard . Can be distinguished by isDaemon () method are: if it returns false, it indicates that the thread is "user thread"; otherwise, it is "daemon thread."
User Thread general users to perform user level tasks, and a daemon thread is the "background threads" is generally used to perform background tasks. Note that: Java Virtual Machine in the "User thread" after the end of all exits.

   Daemon thread called "background threads", "wizard thread", it has a feature - if all foreground threads have died, automatic background thread death .

  To set up a thread through setDaemon (true).

Guess you like

Origin www.cnblogs.com/daiyungongsi01/p/11041027.html