Java core knowledge points

4.1.1. JAVA concurrent Knowledge

2019 Interview essential: the latest Java core knowledge points (3) -JAVA multithreading on

 

4.1.2. JAVA thread implementation / creation mode

4.1.2.1. Inherit the Thread class Thread class nature it is to achieve an instance of Runnable interface, representing an instance of a thread. The only way to start the thread is () instance method by start Thread class. start () method is a native method, it will start a new thread, and executes run () method.

2019 Interview essential: the latest Java core knowledge points (3) -JAVA multithreading on

 

4.1.2.2. Implement Runnable. If your class already extends another class, it can not directly extends Thread, this time, you can achieve a Runnable interface.

2019 Interview essential: the latest Java core knowledge points (3) -JAVA multithreading on

 

 

2019 Interview essential: the latest Java core knowledge points (3) -JAVA multithreading on

 

4.1.2.3. ExecutorService, Callable, Future have a return value of the thread has the task of return values ​​must implement the Callable interface similar task no return value must be Runnable interface. After performing Callable task, you can get a Future object, call the get in on the object you can get to the return of the Object Callable task, combined with the thread pool ExecutorService interfaces can be achieved legends have returned results multithreading.

2019 Interview essential: the latest Java core knowledge points (3) -JAVA multithreading on

 

4.1.2.4. These resources are an invaluable resource database connections based on threads and thread pool. So every time you need to create, destroy when not needed, can be very wasteful of resources. Then we can use caching strategy is to use thread pool.

2019 Interview essential: the latest Java core knowledge points (3) -JAVA multithreading on

 

4.1.3. 4 kinds of thread pool

Java thread pool inside the top-level interface is Executor, but is not a strict sense Executor thread pools, but only one thread of execution tool. The real thread pool interface is a ExecutorService.

2019 Interview essential: the latest Java core knowledge points (3) -JAVA multithreading on

 

4.1.3.1. newCachedThreadPool 创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们。对于执行 很多短期异步任务的程序而言,这些线程池通常可提高程序性能。调用 execute 将重用以前构造 的线程(如果线程可用)。如果现有线程没有可用的,则创建一个新线程并添加到池中。终止并 从缓存中移除那些已有 60 秒钟未被使用的线程。因此,长时间保持空闲的线程池不会使用任何资 源。

4.1.3.2. newFixedThreadPool 创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程。在任意点,在大 多数 nThreads 线程会处于处理任务的活动状态。如果在所有线程处于活动状态时提交附加任务, 则在有可用线程之前,附加任务将在队列中等待。如果在关闭前的执行期间由于失败而导致任何 线程终止,那么一个新线程将代替它执行后续的任务(如果需要)。在某个线程被显式地关闭之 前,池中的线程将一直存在。

4.1.3.3. newScheduledThreadPool 创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。

2019 Interview essential: the latest Java core knowledge points (3) -JAVA multithreading on

 

4.1.3.4. newSingleThreadExecutor Executors.newSingleThreadExecutor()返回一个线程池(这个线程池只有一个线程),这个线程 池可以在线程死后(或发生异常时)重新启动一个线程来替代原来的线程继续执行下去!

4.1.4. 线程生命周期(状态)

当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态。 在线程的生命周期中,它要经过新建(New)、就绪(Runnable)、运行(Running)、阻塞 (Blocked)和死亡(Dead)5 种状态。尤其是当线程启动以后,它不可能一直"霸占"着 CPU 独自 运行,所以 CPU 需要在多条线程之间切换,于是线程状态也会多次在运行、阻塞之间切换

4.1.4.1. 新建状态(NEW) 当程序使用 new 关键字创建了一个线程之后,该线程就处于新建状态,此时仅由 JVM 为其分配 内存,并初始化其成员变量的值

4.1.4.2. 就绪状态(RUNNABLE): 当线程对象调用了 start()方法之后,该线程处于就绪状态。Java 虚拟机会为其创建方法调用栈和 程序计数器,等待调度运行。

4.1.4.3. 运行状态(RUNNING): 如果处于就绪状态的线程获得了 CPU,开始执行 run()方法的线程执行体,则该线程处于运行状 态。

4.1.4.4. 阻塞状态(BLOCKED): 阻塞状态是指线程因为某种原因放弃了 cpu 使用权,也即让出了 cpu timeslice,暂时停止运行。 直到线程进入可运行(runnable)状态,才有机会再次获得 cpu timeslice 转到运行(running)状 态。阻塞的情况分三种:

等待阻塞(o.wait->等待对列): 运行(running)的线程执行 o.wait()方法,JVM 会把该线程放入等待队列(waitting queue) 中。

同步阻塞(lock->锁池) 运行(running)的线程在获取对象的同步锁时,若该同步锁被别的线程占用,则 JVM 会把该线 程放入锁池(lock pool)中。

其他阻塞(sleep/join) 运行(running)的线程执行 Thread.sleep(long ms)或 t.join()方法,或者发出了 I/O 请求时, JVM 会把该线程置为阻塞状态。当 sleep()状态超时、join()等待线程终止或者超时、或者 I/O 处理完毕时,线程重新转入可运行(runnable)状态。

4.1.4.5. 线程死亡(DEAD) 线程会以下面三种方式结束,结束后就是死亡状态。

正常结束 1. run()或 call()方法执行完成,线程正常结束。

异常结束 2. 线程抛出一个未捕获的 Exception 或 Error。

调用 stop 3. 直接调用该线程的 stop()方法来结束该线程—该方法通常容易导致死锁,不推荐使用。

2019 Interview essential: the latest Java core knowledge points (3) -JAVA multithreading on

 

4.1.5. 终止线程 4 种方式

4.1.5.1. 正常运行结束 程序运行结束,线程自动结束。

4.1.5.2. 使用退出标志退出线程 一般 run()方法执行完,线程就会正常结束,然而,常常有些线程是伺服线程。它们需要长时间的 运行,只有在外部某些条件满足的情况下,才能关闭这些线程。使用一个变量来控制循环,例如: 最直接的方法就是设一个 boolean 类型的标志,并通过设置这个标志为 true 或 false 来控制 while 循环是否退出,代码示例:

 

2019 Interview essential: the latest Java core knowledge points (3) -JAVA multithreading on

 

定义了一个退出标志 exit,当 exit 为 true 时,while 循环退出,exit 的默认值为 false.在定义 exit 时,使用了一个 Java 关键字 volatile,这个关键字的目的是使 exit 同步,也就是说在同一时刻只 能由一个线程来修改 exit 的值。

4.1.5.3. Interrupt 方法结束线程 使用 interrupt()方法来中断线程有两种情况:

1. 线程处于阻塞状态:如使用了 sleep,同步锁的 wait,socket 中的 receiver,accept 等方法时, 会使线程处于阻塞状态。当调用线程的 interrupt()方法时,会抛出 InterruptException 异常。 阻塞中的那个方法抛出这个异常,通过代码捕获该异常,然后 break 跳出循环状态,从而让 我们有机会结束这个线程的执行。通常很多人认为只要调用 interrupt 方法线程就会结束,实 际上是错的, 一定要先捕获 InterruptedException 异常之后通过 break 来跳出循环,才能正 常结束 run 方法。

2. 线程未处于阻塞状态:使用 isInterrupted()判断线程的中断标志来退出循环。当使用 interrupt()方法时,中断标志就会置 true,和使用自定义的标志来控制循环是一样的道理。

2019 Interview essential: the latest Java core knowledge points (3) -JAVA multithreading on

 

4.1.5.4. stop 方法终止线程(线程不安全) 程序中可以直接使用 thread.stop()来强行终止线程,但是 stop 方法是很危险的,就象突然关 闭计算机电源,而不是按正常程序关机一样,可能会产生不可预料的结果,不安全主要是: thread.stop()调用之后,创建子线程的线程就会抛出 ThreadDeatherror 的错误,并且会释放子 线程所持有的所有锁。一般任何进行加锁的代码块,都是为了保护数据的一致性,如果在调用 thread.stop()后导致了该线程所持有的所有锁的突然释放(不可控制),那么被保护数据就有可能呈 现不一致性,其他线程在使用这些被破坏的数据时,有可能导致一些很奇怪的应用程序错误。因 此,并不推荐使用 stop 方法来终止线程。

4.1.6. sleep 与 wait 区别

1. 对于 sleep()方法,我们首先要知道该方法是属于 Thread 类中的。而 wait()方法,则是属于 Object 类中的。

2. sleep()方法导致了程序暂停执行指定的时间,让出 cpu 该其他线程,但是他的监控状态依然 保持者,当指定的时间到了又会自动恢复运行状态。

3. 在调用 sleep()方法的过程中,线程不会释放对象锁。

4. 而当调用 wait()方法的时候,线程会放弃对象锁,进入等待此对象的等待锁定池,只有针对此 对象调用 notify()方法后本线程才进入对象锁定池准备获取对象锁进入运行状态。

4.1.7. start 与 run 区别

1. start()方法来启动线程,真正实现了多线程运行。这时无需等待 run 方法体代码执行完毕, 可以直接继续执行下面的代码。

2. Start a thread by calling start () method of the Thread class, then this thread is in the ready state and is not running.

3. The method run () is called thread body, which contains the contents to be executed this thread, the thread into the running state, which runs the run function code. Run method to finish, this thread is terminated. Then CPU rescheduling other threads.

4.1.8. JAVA background thread

1. Definitions: a daemon thread - also known as "service thread", he is a background thread, it has a feature that is user threads to provide public services, will automatically leave when no user serviceable thread.

2. Priority: daemon thread of low priority, be used to provide services to other objects and threads in the system.

3. Set: set to the thread to "daemon thread" by setDaemon (to true); set a user thread is a daemon thread before the thread manner setDaemon method object is created thread object.

4. created a new thread in the thread also Daemon Daemon's.

The thread is JVM-level to Tomcat, for example, if you start a thread in the Web application, the thread of the life cycle and will not keep pace and Web applications. In other words, even if you stop a Web application, this thread is still active.

6. example: garbage collection thread is a classic daemon thread, when we no longer have any program running Thread, the program will not produce garbage, the garbage collector will have nothing to do, so when the garbage collector thread JVM is the only remaining thread, the thread will leave garbage collection automatically. It always runs at a low level state, recyclable resources for real-time monitoring and management systems. 7. Life Cycle: Daemon (Daemon) is running a special process in the background. It is independent of the control terminal and periodically perform some task or some events awaiting processing. That daemon thread is not dependent on the terminal, but dependent on the system, and the system "live and die together." When the JVM all the threads are daemon threads, JVM can be pulled out; if there is one or more non-daemon thread the JVM will not quit.

Guess you like

Origin www.cnblogs.com/xuwenming/p/10981340.html