java multithreading novella (a) - Thread Details

Brief introduction

  In short, the current JDK threading model-based operating system native threads, the model relies on operating system support for threads, in addition to Windows and Linux threading model is to provide one to one
  It is simply that:
    Now Java threads and operating system threads to-one mapping
    Now Java threads, that is, the operating system thread
 
Some are the Thread : the Thread is an abstract description of the Java thread, it must be multi-threaded programming model for the Thread. Regardless of the operating system JVM thread in the end how to map, in the end how to run the face of Java, programmers can see just Thread.

Multi-threaded development is manipulating Thread object :

  Thread the abstract model to simply say that this is the Thread class, he has a property field, there are public methods, but also package the threaded tasks (Runnable), for setting a Thread, he can configure those items, but not too much
   So as long as you understand the abstract model of Thread, then you will be able to clear the Thread you need to configure.
   The Solutions multi-threaded programming model is like playing chess during routine like that, you can make better and faster and more comprehensive write multithreaded applications.
Create a Thread object, use the Thread object , it appears to be unusually simple, then not. There are several ways to manipulate threads, since it is multi-threaded on more than one thread, multi-threaded, multi-interactive methods, will inevitably produce a variety of complex collaboration logic, how can we correct and efficient organization, also is the test of intelligence. 

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?

  So much so that, in the end Java multi-threaded programming in the end is what? We said earlier that only two steps " to create a Thread object, use the Thread object ."
  These two parts are the control and treatment of the thread itself, but when using the Thread object, there is a most important part, it is why we want to use threads?
  Not to perform the task?
    Therefore, the mission of the code is also very important, because, multi-threaded code is no longer a simple task so simple, because if it comes to access to shared resources, so the problem is related to the process of synchronization.
  Learning java multithreading, if you understand what parts will be much better.
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

Include the following 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。

调用start方法启动后,会调用run方法,如果不重写run方法,或者构造时不进程传递,那么target为null
很显然如果target,run方法就相当于一个空方法,也就是什么都不做。

 

 

 简言之,Java对于线程以及线程任务,进行了抽象分离,对线程的抽象为Thread,而对于线程任务的抽象就是Runnable


 

总结

  Thread中的方法主要用于对线程进行控制也可以用作通信,还有一些是基于类设计层面的,添加进来的一些工具类,可以对线程的一些信息进行控制、获取 线程任务是通过Runnable进行抽象,简言之,
  Thread表示线程,Runnable表示任务。
  “分别是为了更好地重逢”放到这里非常合适,解耦是为了更好地协作
  线程本身线程需要执行的任务进行分离,无论是从抽象概念上还是认知理解上,亦或者是二者独立的发展上,解耦都有多种好处。
    

 

 在Java这一面向对象的语言中,多线程编程就是“多Thread对象编程”

 

感谢:https://www.cnblogs.com/noteless/p/10354699.html

Guess you like

Origin www.cnblogs.com/FondWang/p/11982795.html