java|01.JAVA并发编程-线程的使用-基本概念

线程定义 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元。一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成。如果没有明确的协同机制,线程将彼此独立执行。每一个程序都至少有一个线程,若程序只有一个线程,那就是程序本身。
线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的全部资源(共享进程的内存地址空间),因此这些线程都能访问相同的变量并在同一个堆上分配对象,这就需要实现一种比在进程间共享数据粒度更细的数据共享机制。如果没有这种同步机制,在多线程的情况下会出现无法预料的后果。
一个线程可以创建和撤消另一个线程,同一进程中的多个线程之间可以并发执行。由于线程之间的相互制约,致使线程在运行中呈现出间断性。线程也有就绪、阻塞和运行三种基本状态。就绪状态是指线程具备运行的所有条件,逻辑上可以运行,在等待处理机;运行状态是指线程占有处理机正在运行;阻塞状态是指线程在等待一个事件(如某个信号量),逻辑上不可执行。
线程是程序中一个单一的顺序控制流程。进程内一个相对独立的、可调度的执行单元,是系统独立调度和分派CPU的基本单位指运行中的程序的调度单位。在单个程序中同时运行多个线程完成不同的工作,称为多线程。
java中的线程 在java中,一个应用程序对应者一个jvm实例(jvm进程),一般来说名字默认为java.exe或者javaw.exe。java采用的是单线程编程模型,即在我们自己的程序中如果没有主动创建线程,则只会创建一个主线程。但是需要注意,虽然只是一个线程执行任务,不代表jvm中只有一个线程,jvm在创建实例的过程中,同时会创建很多线程,具体参考如下说明:
一个Java程序的入口是main方法,通过调用main方法开始执行,然后按照代码逻辑执行,看似没有其他线程参与,其实java程序天生就是多线程程序,执行一个main方法其实就是一个名为mian的线程和其他线程分别执行,参考代码如下:
代码(参考java并发编程艺术)

package com.sunld; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; /** * @Title: TestMainThread.java * @Package com.sunld * Description:
* @author sunld * @version V1.0.0 * CreateDate:2017年9月28日 下午3:54:19
*/public class TestMainThread {public static void main(String[] args) { // 获取Java线程管理MXBean ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); // 不需要获取同步的monitor和synchronizer信息,仅获取线程和线程堆栈信息 ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false); // 遍历线程信息,仅打印线程ID和线程名称信息 for (ThreadInfo threadInfo : threadInfos) { System.out.println("[" + threadInfo.getThreadId() + "] " + threadInfo.getThreadName()); } } }

返回结果
[5] Attach Listener//附加监听 [4] Signal Dispatcher//分发处理发送给JVM信号的线程 [3] Finalizer//调用对象finalize方法的线程 [2] Reference Handler清除Reference的线程 [1] mainmain线程,用户程序入口

线程状态 源码定义
/** * A thread state.A thread can be in one of the following states: *
    *
  • {@link #NEW}
    *A thread that has not yet started is in this state. *
  • *
  • {@link #RUNNABLE}
    *A thread executing in the Java virtual machine is in this state. *
  • *
  • {@link #BLOCKED}
    *A thread that is blocked waiting for a monitor lock *is in this state. *
  • *
  • {@link #WAITING}
    *A thread that is waiting indefinitely for another thread to *perform a particular action is in this state. *
  • *
  • {@link #TIMED_WAITING}
    *A thread that is waiting for another thread to perform an action *for up to a specified waiting time is in this state. *
  • *
  • {@link #TERMINATED}
    *A thread that has exited is in this state. *
  • *
* * * A thread can be in only one state at a given point in time. * These states are virtual machine states which do not reflect * any operating system thread states. * * @since1.5 * @see #getState */ public enum State { /** * Thread state for a thread which has not yet started. */ NEW,/** * Thread state for a runnable thread.A thread in the runnable * state is executing in the Java virtual machine but it may * be waiting for other resources from the operating system * such as processor. */ RUNNABLE,/** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state is waiting for a monitor lock * to enter a synchronized block/method or * reenter a synchronized block/method after calling * {@link Object#wait() Object.wait}. */ BLOCKED,/** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: *
    *
  • {@link Object#wait() Object.wait} with no timeout
  • *
  • {@link #join() Thread.join} with no timeout
  • *
  • {@link LockSupport#park() LockSupport.park}
  • *
* * A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called Object.wait() * on an object is waiting for another thread to call * Object.notify() or Object.notifyAll() on * that object. A thread that has called Thread.join() * is waiting for a specified thread to terminate. */ WAITING,/** * Thread state for a waiting thread with a specified waiting time. * A thread is in the timed waiting state due to calling one of * the following methods with a specified positive waiting time: *
    *
  • {@link #sleep Thread.sleep}
  • *
  • {@link Object#wait(long) Object.wait} with timeout
  • *
  • {@link #join(long) Thread.join} with timeout
  • *
  • {@link LockSupport#parkNanos LockSupport.parkNanos}
  • *
  • {@link LockSupport#parkUntil LockSupport.parkUntil}
  • *
*/ TIMED_WAITING,/** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }

状态转换图

java|01.JAVA并发编程-线程的使用-基本概念
文章图片


状态说明

java|01.JAVA并发编程-线程的使用-基本概念
文章图片


当需要新起一个线程来执行某个子任务时,就创建了一个线程。但是线程创建之后,不会立即进入就绪状态,因为线程的运行需要一些条件(比如内存资源,程序计数器、Java栈、本地方法栈都是线程私有的,所以需要为线程分配一定的内存空间),只有线程运行需要的所有条件满足了,才进入就绪状态。
当线程进入就绪状态后,不代表立刻就能获取CPU执行时间,也许此时CPU正在执行其他的事情,因此它要等待。当得到CPU执行时间之后,线程便真正进入运行状态。
线程在运行状态过程中,可能有多个原因导致当前线程不继续运行下去,比如用户主动让线程睡眠(睡眠一定的时间之后再重新执行)、用户主动让线程等待,或者被同步块给阻塞,此时就对应着多个状态:time waiting(睡眠或等待一定的事件)、waiting(等待被唤醒)、blocked(阻塞)。
当由于突然中断或者子任务执行完毕,线程就会被消亡。
Java多线程的就绪、运行和死亡状态 就绪状态转换为运行状态:当此线程得到处理器资源;
运行状态转换为就绪状态:当此线程主动调用yield()方法或在运行过程中失去处理器资源。
运行状态转换为死亡状态:当此线程线程执行体执行完毕或发生了异常。
此处需要特别注意的是:当调用线程的yield()方法时,线程从运行状态转换为就绪状态,但接下来CPU调度就绪状态中的哪个线程具有一定的随机性,因此,可能会出现A线程调用了yield()方法后,接下来CPU仍然调度了A线程的情况。
由于实际的业务需要,常常会遇到需要在特定时机终止某一线程的运行,使其进入到死亡状态。目前最通用的做法是设置一boolean型的变量,当条件满足时,使线程执行体快速执行完毕(不在执行run方法)在后续的文章中会介绍如何安全的终止一个线程。。
状态分析–jvisualvm 代码-参考(java并发编程艺术)
package com.sunld; import java.util.concurrent.TimeUnit; /** * @Title: TestThreadState.java * @Package com.sunld * Description:
* @author sunld * @version V1.0.0 * CreateDate:2017年9月28日 下午5:14:27
*/public class TestThreadState { public static void main(String[] args) { new Thread(new TimeWaiting (), "TimeWaitingThread").start(); new Thread(new Waiting(), "WaitingThread").start(); // 使用两个Blocked线程,一个获取锁成功,另一个被阻塞 new Thread(new Blocked(), "BlockedThread-1").start(); new Thread(new Blocked(), "BlockedThread-2").start(); } //该线程不断地进行睡眠 static class TimeWaiting implements Runnable{ @Override public void run() { SleepUtils.second(100); } } //该线程在Waiting.class实例上等待 static class Waiting implements Runnable{@Override public void run() { while (true) { synchronized (Waiting.class) { try { Waiting.class.wait(); }catch(InterruptedException e) { e.printStackTrace(); } } } }} //该线程在Blocked.class实例上加锁后,不会释放该锁 static class Blocked implements Runnable {@Override public void run() { synchronized (Blocked.class) { while (true) { SleepUtils.second(100); } } } } } class SleepUtils{ public static final void second(long seconds) { try { TimeUnit.SECONDS.sleep(seconds); }catch(InterruptedException e) { e.printStackTrace(); } } }

dump结果
2017-09-28 17:26:47 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.112-b15 mixed mode)://BlockedThread-2线程获取到了Blocked.class的锁 "BlockedThread-2" #13 prio=5 os_prio=0 tid=0x000000001f268000 nid=0x3754 waiting on condition [0x000000002009f000] java.lang.Thread.State: TIMED_WAITING (sleeping)//BlockedThread-1线程阻塞在获取Blocked.class示例的锁上 "BlockedThread-1" #12 prio=5 os_prio=0 tid=0x000000001f266800 nid=0x89c waiting for monitor entry [0x000000001ff9f000] java.lang.Thread.State: BLOCKED (on object monitor)//WaitingThread线程在Waiting实例上等待 "WaitingThread" #11 prio=5 os_prio=0 tid=0x000000001f260800 nid=0x4d08 in Object.wait() [0x000000001fe9f000] java.lang.Thread.State: WAITING (on object monitor)//TimeWaitingThread线程处于超时等待 "TimeWaitingThread" #10 prio=5 os_prio=0 tid=0x000000001f25f000 nid=0x42ac waiting on condition [0x000000001fd9e000] java.lang.Thread.State: TIMED_WAITING (sleeping)

线程优先级 现代操作系统基本采用时分的形式调度运行的线程,操作系统会分出一个个时间片,线程会分配到若干时间片,当线程的时间片用完了就会发生线程调度,并等待着下次分配。线程分配到的时间片多少也就决定了线程使用处理器资源的多少,而线程优先级就是决定线程需要多或者少分配一些处理器资源的线程属性。优先级高的线程分配时间片的数量要多于优先级低的线程。
设置方式setPriority(int)
#属性定义 private intpriority; /** * The minimum priority that a thread can have. */ public final static int MIN_PRIORITY = 1; /** * The default priority that is assigned to a thread. */ public final static int NORM_PRIORITY = 5; /** * The maximum priority that a thread can have. */ public final static int MAX_PRIORITY = 10;

/** * Changes the priority of this thread. * * First the checkAccess method of this thread is called * with no arguments. This may result in throwing a * SecurityException. * * Otherwise, the priority of this thread is set to the smaller of * the specified newPriority and the maximum permitted * priority of the thread's thread group. * * @param newPriority priority to set this thread to * @exceptionIllegalArgumentExceptionIf the priority is not in the *range MIN_PRIORITY to *MAX_PRIORITY. * @exceptionSecurityExceptionif the current thread cannot modify *this thread. * @see#getPriority * @see#checkAccess() * @see#getThreadGroup() * @see#MAX_PRIORITY * @see#MIN_PRIORITY * @seeThreadGroup#getMaxPriority() */ public final void setPriority(int newPriority) { ThreadGroup g; checkAccess(); if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) { throw new IllegalArgumentException(); } if((g = getThreadGroup()) != null) { if (newPriority > g.getMaxPriority()) { newPriority = g.getMaxPriority(); } setPriority0(priority = newPriority); } }

参考实例(java并发编程艺术) 设置线程优先级时,针对频繁阻塞(休眠或者I/O操作)的线程需要设置较高优先级,而偏重计算(需要较多CPU时间或者偏运算)的线程则设置较低的优先级,确保处理器不会被独占。在不同的JVM以及操作系统上,线程规划会存在差异,有些操作系统甚至会忽略对线程优先级的设定。
实例
package com.sunld; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; /** * @Title: Priority.java * @Package com.sunld * Description:
* @author sunld * @version V1.0.0 * CreateDate:2017年10月9日 下午2:26:33
*/public class Priority { private static volatile boolean notStart = true; private static volatile boolean notEnd = true; public static void main(String[] args) throws InterruptedException { List jobs = new ArrayList(); for(int i = 0; i < 10; i++) { int priority = i < 5 ? Thread.MIN_PRIORITY : Thread.MAX_PRIORITY; Job job = new Job(priority); jobs.add(job); Thread thread = new Thread(job, "Thread:" + i); thread.setPriority(priority); thread.start(); } notStart = false; TimeUnit.SECONDS.sleep(10); notEnd = false; for (Job job : jobs) { System.out.println("Job Priority : " + job.priority + "," + "Count : " + job.jobCount); } } static class Job implements Runnable{ private int priority; private long jobCount; public Job(int priority) { this.priority = priority; } @Override public void run() { while(notStart) { Thread.yield(); } while(notEnd) { Thread.yield(); jobCount++; } } } }

运行结果
Job Priority : 1,Count : 1034791 Job Priority : 1,Count : 1034727 Job Priority : 1,Count : 1033945 Job Priority : 1,Count : 1034445 Job Priority : 1,Count : 1034565 Job Priority : 10,Count : 4388204 Job Priority : 10,Count : 4374055 Job Priority : 10,Count : 4375073 Job Priority : 10,Count : 4350174 Job Priority : 10,Count : 4393224

结论
测试环境是win10+JDK1.8,级别越高执行的次数越多。但是线程优先级不能作为程序正确性的依赖,因为操作系统可以完全不用理会Java线程对于优先级的设定。
线程优势 如果使用得当,线程可以有效地的降低程序的开发和维护成本,同时提升复杂应用程序的性能。线程能够将大部分的异步工作流转成串行工作流,因此能更好的模拟人类的工作方式和交互方式。此外,线程可以降低代码的复杂度,是代码更容易编写、阅读和维护。
在GUI应用中,线程可以提高用户界面的响应灵敏度,而在服务器应用程序中,可以提升资源利用率以及系统吞吐率。线程还可以简化jvm的实现,垃圾收集器通常在一个或多个专门的线程中运行。
发挥多处理器的强大能力 随着技术的发展,在单核处理器上通过提高时钟频率来提升性能以变的越来越困难,目前大多数机器都是在单个芯片上放置多个处理器核。但是由于基本的调度单位是线程,因此如果使用单个线程会造成很多cpu资源的浪费。多线程程序可以同时在多个处理器上执行,并且可以通过提高处理器资源的利用率来提升吞吐率。比如IO读写。
建模的简单性 通过使用线程,可以将复杂并且异步的工作流进一步分解为一组简单并且同步的工作流,每个工作流在一个单独的线程中运行,并在特定的同步位置进行交互。
可以使用框架实现以上功能,例如:servlet和RMI;框架负责解决一些细节问题,例如请求管理、线程创建、负载均衡,并在正确的时刻将请求分发给正确的应用程序组件。
Java为多线程编程提供了良好、考究并且一致的编程模型,使开发人员能够更加专注于问题的解决,即为所遇到的问题建立合适的模型,而不是绞尽脑汁地考虑如何将其多线程化。一旦开发人员建立好了模型,稍做修改总是能够方便地映射到Java提供的多线程编程模型上。
异步事件的简化处理 服务器应用程序在接受来自多个远程客户端的套接字链接请求时,如果为每个链接都分配其各自的线程并且使用同步IO,那么就会降低这类程序的开发难度。
响应更灵敏的用户界面 使用多线程技术,即将数据一致性不强的操作派发给其他线程处理(也可以使用消息队列),如生成订单快照、发送邮件等。这样做的好处是响应用户请求的线程能够尽可能快地处理完成,缩短了响应时间,提升了用户体验。
线程问题 安全性问题 活跃性问题 性能问题 守护线程(Daemon Thread) java中线程的分类:用户线程 (User Thread)、守护线程 (Daemon Thread)。
所谓守护 线程,是指在程序运行的时候在后台提供一种通用服务的线程,比如垃圾回收线程就是一个很称职的守护者,并且这种线程并不属于程序中不可或缺的部分。因此,当所有的非守护线程结束时,程序也就终止了,同时会杀死进程中的所有守护线程。反过来说,只要任何非守护线程还在运行,程序就不会终止。
区别:用户线程和守护线程两者几乎没有区别,唯一的不同之处就在于虚拟机的离开:如果用户线程已经全部退出运行了,只剩下守护线程存在了,虚拟机也就退出了。 因为没有了被守护者,守护线程也就没有工作可做了,也就没有继续运行程序的必要了。
定义 通过Thread中的setDaemon方法完成设置。通过源码分析得知:当线程只剩下守护线程的时候,JVM就会退出.但是如果还有其他的任意一个用户线程还在,JVM就不会退出
/** * Marks this thread as either a {@linkplain #isDaemon daemon} thread * or a user thread. The Java Virtual Machine exits when the only * threads running are all daemon threads. * * This method must be invoked before the thread is started. * * @paramon *if {@code true}, marks this thread as a daemon thread * * @throwsIllegalThreadStateException *if this thread is {@linkplain #isAlive alive} * * @throwsSecurityException *if {@link #checkAccess} determines that the current *thread cannot modify this thread */ public final void setDaemon(boolean on) { checkAccess(); if (isAlive()) { throw new IllegalThreadStateException(); } daemon = on; }

实例:jvm退出演示
package com.sunld; /** * @Title: MyDaemon.java * @Package com.sunld * Description:
* @author sunld * @version V1.0.0 * CreateDate:2017年10月9日 下午4:09:31
*/public class MyDaemon implements Runnable{public static void main(String[] args) { Thread daemonThread = new Thread(new MyDaemon()); // 设置为守护进程 daemonThread.setDaemon(true); daemonThread.start(); System.out.println("isDaemon = " + daemonThread.isDaemon()); //sleep完成之后,main线程结束,JVM退出! try { Thread.sleep(30000); } catch (InterruptedException e) { e.printStackTrace(); }//AddShutdownHook方法增加JVM停止时要做处理事件: //当JVM退出时,打印JVM Exit语句. Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { System.out.println("JVM Exit!"); }}); }@Override public void run() { for(int i = 0; i < 10; i++) { System.out.println(i+"=====MyDaemon======="); } } }

注意事项
(1) thread.setDaemon(true)必须在thread.start()之前设置,否则会跑出一个IllegalThreadStateException异常。你不能把正在运行的常规线程设置为守护线程。
(2) 在Daemon线程中产生的新线程也是Daemon的。
(3) 守护线程应该永远不去访问固有资源,如文件、数据库,因为它会在任何时候甚至在一个操作的中间发生中断。
(4)Daemon线程被用作完成支持性工作,但是在Java虚拟机退出时Daemon线程中的finally块并不一定会执行,所以不能依靠finally块中的内容来确保执行关闭或清理资源的逻辑

参考资料 【java|01.JAVA并发编程-线程的使用-基本概念】Java中的多线程你只要看这一篇就够了
线程
Java总结篇系列:Java多线程(一)
《java并发编程艺术》
《java并发编程实战》
多线程01:《疯狂Java讲义》学习笔记——线程概述
java并发编程—如何创建线程以及Thread类的使用
Java并发编程:Thread类的使用

    推荐阅读