Java multi-threading summary (a) multi-thread basis

  Multithreading is a very important aspect of learning Java, Java is that each programmer must master basic skills. This article is a multi-threaded details, summarize the essence, there is no entry code for example, are not suitable for beginners to understand. For beginners to learn multi-threaded, it is recommended while reading, watching blog, try to write code.

  Please indicate the source --http: //www.cnblogs.com/zrtqsk/p/3776328.html Thank you!

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 with other processes together, according to their 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  

   https://images0.cnblogs.com/blog/497634/201312/18152411-a974ea82ebc04e72bd874c3921f8bfec.jpg

 

(Photo Source: http: //www.cnblogs.com/skywang12345/p/3479024.html)

 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. 就绪状态(Runnable): 也被称为“可执行状态”。线程对象被调用了该对象的start()方法,该线程处于就绪状态。Java虚拟机会为其创建方法调用栈和程序计数器。处于就绪状态的线程,随时可能被CPU调度执行,取决于JVM中线程调度器的调度。

3. 运行状态(Running) : 线程获取CPU权限进行执行。需要注意的是,线程只能从就绪状态进入到运行状态。

4. 阻塞状态(Blocked)  : 阻塞状态是线程因为某种原因放弃CPU使用权,暂时停止运行。直到线程进入就绪状态,才有机会转到运行状态。阻塞的情况分三种:
    (01) 等待阻塞 -- 通过调用线程的wait()方法,让线程等待某工作的完成。
    (02) 同步阻塞 -- 线程在获取synchronized同步锁失败(因为锁被其它线程所占用),它会进入同步阻塞状态。
    (03) 其他阻塞 -- 通过调用线程的sleep()或join()或发出了I/O请求时,线程会进入到阻塞状态。当sleep()状态超时、join()等待线程终止或者超时、或者I/O处理完毕时,线程重新转入就绪状态。

5. 死亡状态(Dead)    : 线程执行完了、因异常退出了run()方法或者直接调用该线程的stop()方法(容易导致死锁,现在已经不推荐使用),该线程结束生命周期。

 

 

四、wait()、notify()、nofityAll()方法 

  在Object.java中,定义了wait(), notify()和notifyAll()等方法。

  wait()的作用是让当前线程进入等待状态,同时,wait()也会让当前线程释放它所持有的锁

  而 notify()和notifyAll()的作用,则是唤醒当前对象上的等待线程;notify()是唤醒单个线程,而notifyAll()是唤醒所有的线程。

Object类中关于等待/唤醒的API详细信息如下:
  notify()        -- 唤醒在此对象监视器上等待的单个线程,使其进入“就绪状态”。  
  notifyAll()   -- 唤醒在此对象监视器上等待的所有线程,使其进入“就绪状态”
  wait()                                     -- 让当前线程处于“等待(阻塞)状态”,“直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法”,当前线程被唤醒(进入“就绪状态”)。
  wait(long timeout)                 -- 让当前线程处于“等待(阻塞)状态”,“直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量”,当前线程被唤醒(进入“就绪状态”)。
  wait(long timeout, int nanos) -- 让当前线程处于“等待(阻塞)状态”,“直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者其他某个线程中断当前线程,或者已超过某个实际时间量”,当前线程被唤醒(进入“就绪状态”)。

 wait()的作用是让“当前线程”等待(会释放锁),而“当前线程”是指正在cpu上运行的线程!

 此处,http://www.cnblogs.com/skywang12345/p/3479224.html例子讲的非常详细。

 

 

五、yield()、sleep()、join()和interrupt()方法  

1、yield() 

  yield()是Thread类的静态方法。它能让当前线程暂停,但不会阻塞该线程,而是由“运行状态”进入到“就绪状态”,从而让 其它具有相同优先级的等待线程获取执行权;但是,并不能保证在当前线程调用yield()之后,其它具有相同优先级的线程就一定能获得执行权;也有可能是 当前线程又进入到“运行状态”继续运行!

  值得注意的是,yield()方法不会释放锁

 

2、sleep()

  sleep()是Thread类的静态方法。该方法声明抛出了InterrupedException异常。所以使用时,要么捕捉,要么声明抛出。

  有2种重载方式:

——static void sleep(long millis)  :  让当前正在执行的线程暂停millis毫秒,并进入阻塞状态,该方法受到系统计时器和线程调度器的精度和准度的影响。

——static void sleep(long millis , int nanos)  :  让当前正在执行的线程暂停millis毫秒加nanos微秒,并进入阻塞状态,该方法受到系统计时器和线程调度器的精度和准度的影响。

  sleep() 的作用是让当前线程休眠,即当前线程会从“运行状态”进入到“休眠(阻塞)状态”。sleep()会指定休眠时间,线程休眠的时间会大于/等于该休眠时间;在线程重新被唤醒时,它会由“阻塞状态”变成“就绪状态”,从而等待cpu的调度执行。常用来暂停程序的运行。  

  同时注意,sleep()方法不会释放锁

 

3、join()

  join() 是Thread的一个实例方法。表示,当某个程序执行流中调用其他线程的join方法时,调用线程将被阻塞,直到被join的线程执行完毕。

有3种重载的形式:

——join()  :  等待被join的线程执行完成

——join(long millis)  :  等待被join的线程的时间最长为millis毫秒,若在millis毫秒内,被join的线程还未执行结束,则不等待。

——join(long millis , int nanos)  :  等待被join的线程的时间最长为millis毫秒加nanos微秒,若在此时间内,被join的线程还未执行结束,则不等待。

即当前线程内,用某个线程对象调用join()后,会使当前线程等待,直到该线程对象的线程运行完毕,原线程才会继续运行。

 

4、interrupt()   

  我们经常通过判断线程的中断标记来控制线程。   

  interrupt()是Thread类的一个实例方法,用于中断本线程。这个方法被调用时,会立即将线程的中断标志设置为“true”。所以当中断处于“阻塞状态”的线程时,由于处于阻塞状态,中断标记会被设置为“false”,抛出一个 InterruptedException。所以我们在线程的循环外捕获这个异常,就可以退出线程了。

  interrupt()并不会中断处于“运行状态”的线程,它会把线程的“中断标记”设置为true,所以我们可以不断通过isInterrupted()来检测中断标记,从而在调用了interrupt()后终止线程,这也是通常我们对interrupt()的用法。

  Interrupted()是Thread类的一个静态方法,它返回一个布尔类型指明当前线程是否已经被中断,isInterrupted()是Thread类的实例方法,返回一个布尔类型来判断线程是否已经被中断。它们都能够用于检测对象的“中断标记”。区别是,interrupted()除了返回中断标记之外,它还会清除中断标记(即将中断标记设为false);而isInterrupted()仅仅返回中断标记。

 

 

六、 Synchronized关键字

1、原理

  在java中,每一个对象有且仅有一个同步锁。这也意味着,同步锁是依赖于对象而存在。

  当当前线程调用某对象的synchronized方法时,就获取了该对象的同步锁。例如,synchronized(obj),当前线程就获取了“obj这个对象”的同步锁。

  不同线程对同步锁的访问是互斥的。也就是说,某时间点,对象的同步锁只能被一个线程获取到!通过同步锁,我们就能在多线程中,实现对“对象/方法”的互斥访问。 例如,现在有个线程A和线程B,它们都会访问“对象obj的同步锁”。假设,在某一时刻,线程A获取到“obj的同步锁”并在执行一些操作;而此时,线程B也企图获取“obj的同步锁” —— 线程B会获取失败,它必须等待,直到线程A释放了“该对象的同步锁”之后线程B才能获取到“obj的同步锁”从而才可以运行。

 

2、基本规则

  第一条 :  当一个线程访问“某对象”的“synchronized方法”或者“synchronized代码块”时,其他线程对“该对象”的该“synchronized方法”或者“synchronized代码块”的访问将被阻塞。
  第二条 :  当一个线程访问“某对象”的“synchronized方法”或者“synchronized代码块”时,其他线程仍然可以访问“该对象”的非同步代码块
  第三条 :  当一个线程访问“某对象”的“synchronized方法”或者“synchronized代码块”时,其他线程对“该对象”的其他的“synchronized方法”或者“synchronized代码块”的访问将被阻塞。

 

3、实例锁和全局锁

实例锁 -- 锁在某一个实例对象上。如果该类是单例,那么该锁也具有全局锁的概念。
               实例锁对应的就是synchronized关键字。
全局锁 -- 该锁针对的是类,无论实例多少个对象,那么线程都共享该锁。
               全局锁对应的就是static synchronized(或者是锁在该类的class或者classloader对象上)。

  就是说,一个非静态方法上的synchronized关键字,代表该方法依赖其所属对象。一个静态方法上synchronized关键字,代表该方法依赖这个类本身。

 

 

七、线程优先级和守护线程 

1、线程优先级  

  java中的线程优先级的范围是1~10,默认的优先级是5。每个线程默认的优先级都与创建它的父线程具有相同的优先级。默认情况下,mian线程具有普通优先级。“高优先级线程”会优先于“低优先级线程”执行。Thread提供了setPriority(int newPriority)和getPriority()方法来设置和返回线程优先级。

  Thread类有3个静态常量:

——MAX_PRIORITY = 10

——MIN_PRIORITY = 1

——NORM_PRIORITY = 5

 

2、守护线程

  java 中有两种线程:用户线程守护线程。可以通过isDaemon()方法来区别它们:如果返回false,则说明该线程是“用户线程”;否则就是“守护线程”。
用户线程一般用户执行用户级任务,而守护线程也就是“后台线程”,一般用来执行后台任务。需要注意的是:Java虚拟机在“用户线程”都结束后会后退出。

   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 (to true) .

 

 

 

 About Java and various summaries of knowledge, we recommend a blogger skywang12345 , his knowledge of various Java summary it is easy to understand in detail and classic, gave me a lot of help.

 Reference: http://www.cnblogs.com/skywang12345/p/java_threads_category.html

  "Crazy Java handouts"

 

 If you feel that this article is not bad, then click on the recommendation of trouble Oh! Thank you!

 

Reproduced in: https: //www.cnblogs.com/zrtqsk/p/3776328.html

Guess you like

Origin blog.csdn.net/weixin_33859844/article/details/93248530