Brief introduction
Multi-threaded development is manipulating Thread object :
Java threads logic: thread core of the thread control , thread synchronization , thread communication .
- For the Thread class itself, with its own properties, such as name, Id, priority, status, etc., which is to control the information process;
- For Thread encapsulated task, Java provides synchronized, volatile keyword is used to control access to shared resources, which is the synchronization process;
- The Thread for some of the acts (and inherited from the Object), such as join, wait the equivalent of the communication process.
Java multi-threaded programming in the end what is it?
The first part, a clear understanding of the Java control method of abstract concepts and Thread thread support operation --- that is, the Thread class itself.The second part, a clear understanding of Java synchronization logic, which is synchronized keyword thorough understanding.The third section, for clarity of understanding abstract Java thread communication logic, i.e. correlation methods such as the wait
Thread class
1. Thread Overview
A Thread includes information on three areas: basic information, threading their behavior, threaded tasks
1.1 Basic Information
- Name, id, priority, status, thread group, daemon thread status, and stack trace information
- Context class loader is provided, the exception handler is provided
- It is alive, whether the current thread has permission to modify this thread
Name: thread is the name of the attribute name, if you do not specify a name, it will generate a thread-0, thread-1 ......... thread-N This name
ID: analogy people say, ID is a unique identifier ID number, thread. Thread ID is a positive long number generated when you create the thread, the thread termination, ID can be reused
优先级:线程默认的优先级是NORM_PRIORITY=5,一般情况下不需要设置优先级因为你设置了优先级并不一定总是完全按照你的想法进行,前面说过,Java线程是操作系统原生线程的映射,要依赖操作系统。
如果设置的值不在有效范围内,直接抛出异常。万万不要业务逻辑依赖你自以为的线程优先级
状态:新建、就绪、运行、阻塞、结束
线程组:线程组用于对线程进行管理,ThreadGroup 线程组表示一个线程的集合。此外,线程组也可以包含其他线程组,线程组构成一棵树,在树中,除了初始线程组外,每个线程组都有一个父线程组
守护线程状态:可以将一个Thread标记为守护线程。守护线程,可以认为是后台线程。需要注意的是,必须是线程启动前设置,不然你试试看,分分钟 throw new IllegalThreadStateException()。
堆栈信息跟踪:待更......
上下文类加载器设置:待更新......
异常处理器设置:待更新......
是否存活:线程从启动之后,直到最终终止,这一个过程被称之为是活动状态换句话说,一个线程start之后,除非他被终止,否则任何时刻都是true。isAlive就是用于检测线程是否处于活动状态。
当前线程是否有权修改该线程:判定当前运行的线程是否有权修改该线程。比如线程Thread aThread,在main方法中调用aThread.checkAccess,此时当前线程是主线程main,
目标是aThread 那么就是检测主线程是否有权利修改线程aThread。
1.2 线程行为
Thread中的方法,有一些是线程本身的行为控制或者通信,另外还有一些相当于是工具类。
currentThread: 返回对当前正在执行的线程对象的引用,线程是Thread,哪个Thread正在运行,那么就返回哪个对象就好了,返回类型就是Thread。
activeCount:返回的是当前线程,所在的线程组中,活动线程的个数
enumerate:线程的抽象是Thread,每一个线程都是一个Thread,既然是对象那么就有类似寻常对象的操作,比如保存到数组。enumerate就是用来讲当前线程的、所属线程组中的、以及子组中的每一个活动线程复制到指定的数组中,返回值为复制的线程的个数
是否持有指定监视器的锁:如同前面提到过的互斥量,Java中同步时需要用到一个对象锁,如果一个线程请求的锁被别的线程获得,那么就需要进行等待,持有了锁就可以进入临界区。
方法用于判断当前线程,当前线程、当前线程。针对于某个对象,是否持有对应的锁,当且仅当当前线程在指定的对象上保持监视器锁时,才返回 true。
1.3 线程任务
线程的任务核心是Runnable,内部持有一个Runnable target,构造时如果不进行设置那么为null。
简言之,Java对于线程以及线程任务,进行了抽象分离,对线程的抽象为Thread,而对于线程任务的抽象就是Runnable。
总结
在Java这一面向对象的语言中,多线程编程就是“多Thread对象编程”
感谢:https://www.cnblogs.com/noteless/p/10354699.html