永远不要忘记最基础的东西,只有把最基础的知识打牢靠,才能够使你走的更远,我将从今天开始,进行线程知识的回顾,一些常用知识点,以及java1.5 引入的并发库,进行详细的讲解与总结

创建线程的目的是为了开启一条执行路径,去运行指定的代码;

java 对线程的创建java.lang包下,我们先看关于Thread 源码文档,看它对Thread 有什么解释呢?

  1. * A <i>thread</i> is a thread of execution in a program. The Java
  2. * Virtual Machine allows an application to have multiple threads of
  3. * execution running concurrently.
  4. * <p>
    翻译:
      线程是程序中的执行线程,Java 虚拟机 允许应用的程序去使用多个线程并发的去执行运行
  5. * Every thread has a priority. Threads with higher priority are
  6. * executed in preference to threads with lower priority. Each thread
  7. * may or may not also be marked as a daemon. When code running in
  8. * some thread creates a new <code>Thread</code> object, the new
  9. * thread has its priority initially set equal to the priority of the
  10. * creating thread, and is a daemon thread if and only if the
  11. * creating thread is a daemon.
  12. 翻译:
      每一个线程都是有优先级的,线程高的优先级优于线程低的优先级,每一个线程可以或者不可以标记为一个守护线程
     当一个线程中运行创建了一个新的线程对象,这个新线程的优先级初始值等于创建它的线程的优先级,当创建线程是守护线程的
    时,新线程才是守护程序
  13. * When a Java Virtual Machine starts up, there is usually a single
  14. * non-daemon thread (which typically calls the method named
  15. * <code>main</code> of some designated class). The Java Virtual
  16. * Machine continues to execute threads until either of the following
  17. * occurs:
  18. * <ul>
  19. * <li>The <code>exit</code> method of class <code>Runtime</code> has been
  20. * called and the security manager has permitted the exit operation
  21. * to take place.
  22. * <li>All threads that are not daemon threads have died, either by
  23. * returning from the call to the <code>run</code> method or by
  24. * throwing an exception that propagates beyond the <code>run</code>
  25. * method.
  26.  
  27. 翻译:
      Java 虚拟机 启动的时候,通常都会有单个非守护线程(代表性的他会调用某个类的main 方法)Java 虚拟机会一直运行
    知道下面的情况发生
      1.使用了runtime 类的exit 方法,并且安全管理器允许退出操作发生
      2.非守护线程中的线程停止运行,都会抛出传播到run方法之外的的异常

线程创建的两种方式:官方都有了明确的demo

  1. * There are two ways to create a new thread of execution. One is to
  2. * declare a class to be a subclass of <code>Thread</code>. This
  3. * subclass should override the <code>run</code> method of class
  4. * <code>Thread</code>. An instance of the subclass can then be
  5. * allocated and started. For example, a thread that computes primes
  6. * larger than a stated value could be written as follows:
    翻译:
  7.  
  8. 声明一个继承Thread 的子类,这个子类应该去覆盖run 方法,这个子类的实例会被分配和启动;下面是例子:
  9. * <hr><blockquote><pre>
  10. * class PrimeThread extends Thread {
  11. * long minPrime;
  12. * PrimeThread(long minPrime) {
  13. * this.minPrime = minPrime;
  14. * }
  15. *
  16. * public void run() {
  17. * // compute primes larger than minPrime
  18. * &nbsp;.&nbsp;.&nbsp;.
  19. * }
  20. * }
  21. * </pre></blockquote><hr>
  22. * <p>
  23. * The following code would then create a thread and start it running:
  24. * <blockquote><pre>
  25. * PrimeThread p = new PrimeThread(143);
  26. * p.start();
  27. * </pre></blockquote>
  28. * <p>
  29. * The other way to create a thread is to declare a class that
  30. * implements the <code>Runnable</code> interface. That class then
  31. * implements the <code>run</code> method. An instance of the class can
  32. * then be allocated, passed as an argument when creating
  33. * <code>Thread</code>, and started. The same example in this other
  34. * style looks like the following:
    翻译:
      另一种方式是声明一个实现runnable 接口的类,然后实现run 方法,这个实例会被分配
    在创建线程时候,它会被传递作为一个参数,然后start ,下面是例子
  1. * <hr><blockquote><pre>
  2. * class PrimeRun implements Runnable {
  3. * long minPrime;
  4. * PrimeRun(long minPrime) {
  5. * this.minPrime = minPrime;
  6. * }
  7. *
  8. * public void run() {
  9. * // compute primes larger than minPrime
  10. * &nbsp;.&nbsp;.&nbsp;.
  11. * }
  12. * }
  13. * </pre></blockquote><hr>
  14. * <p>
  15. * The following code would then create a thread and start it running:
  16. * <blockquote><pre>
  17. * PrimeRun p = new PrimeRun(143);
  18. * new Thread(p).start();
  19. * </pre></blockquote>
  20. * <p>
  21. * Every thread has a name for identification purposes. More than
  22. * one thread may have the same name. If a name is not specified when
  23. * a thread is created, a new name is generated for it.
  24. * <p>
  25. * Unless otherwise noted, passing a {@code null} argument to a constructor
  26. * or method in this class will cause a {@link NullPointerException} to be
  27. * thrown.

在下一节,我们对官方提供对两种方式进行实例编写

java 线程Thread 技术--线程创建源码解释的更多相关文章

  1. java 线程Thread 技术--线程状态与同步问题

    线程技术第三篇: 线程的状态: 1. 创建状态: 当用new 操作符创建一个新的线程对象时,该线程就处于创建状态,系统不为它分配资源 2.可运行状态:当线程调用start 方法将为线程分配必须的系统资 ...

  2. java 线程Thread 技术--线程方法详解

    Thread 类常用的方法与Object类提供的线程操作方法:(一个对象只有一把锁

  3. Java并发指南12:深度解读 java 线程池设计思想及源码实现

    ​深度解读 java 线程池设计思想及源码实现 转自 https://javadoop.com/2017/09/05/java-thread-pool/hmsr=toutiao.io&utm_ ...

  4. 【转载】深度解读 java 线程池设计思想及源码实现

    总览 开篇来一些废话.下图是 java 线程池几个相关类的继承结构: 先简单说说这个继承结构,Executor 位于最顶层,也是最简单的,就一个 execute(Runnable runnable) ...

  5. 并发编程(十二)—— Java 线程池 实现原理与源码深度解析 之 submit 方法 (二)

    在上一篇<并发编程(十一)—— Java 线程池 实现原理与源码深度解析(一)>中提到了线程池ThreadPoolExecutor的原理以及它的execute方法.这篇文章是接着上一篇文章 ...

  6. 线程池 ThreadPoolExecutor 原理及源码笔记

    前言 前面在学习 JUC 源码时,很多代码举例中都使用了线程池 ThreadPoolExecutor,并且在工作中也经常用到线程池,所以现在就一步一步看看,线程池的源码,了解其背后的核心原理. 公众号 ...

  7. 线程池 ThreadPoolExecutor 类的源码解析

    线程池 ThreadPoolExecutor 类的源码解析: 1:数据结构的分析: private final BlockingQueue<Runnable> workQueue;  // ...

  8. 《JAVA高并发编程详解》-Thread start方法的源码

    Thread start方法的源码:

  9. Netty中NioEventLoopGroup的创建源码分析

    NioEventLoopGroup的无参构造: public NioEventLoopGroup() { this(0); } 调用了单参的构造: public NioEventLoopGroup(i ...

随机推荐

  1. using关键字在C#中的3种用法

    using 关键字有两个主要用途:  (一).作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型.  (二).作为语句,用于定义一个范围,在此范围的末尾将释放对象. (一).作为指令 1. ...

  2. Grafana+Zabbix使用配置

    官方提供的网友分享的图形面板,可以自行选择使用下载---  https://grafana.com/dashboards   Grafana 是 Graphite 和 InfluxDB 仪表盘和图形编 ...

  3. LeetCode OJ 15. 3Sum

    题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  4. Ubuntu下的LNMP环境

    保证联网的情况下,直接参照http://lnmp.org/install.html进行安装,以下花括号内为原文引用: { 1.使用putty或类似的SSH工具登陆VPS或服务器: 登陆后运行:scre ...

  5. 侧边栏收起展开效果,onmouseover,onmouseout

    //方法一<!doctype html> <html lang="en"> <head> <meta charset="UTF- ...

  6. quartz整合spring框架service层对象注入为null解决方案

    Job实现类代码 package cn.itcast.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; imp ...

  7. hbase高可用集群部署(cdh)

    一.概要 本文记录hbase高可用集群部署过程,在部署hbase之前需要事先部署好hadoop集群,因为hbase的数据需要存放在hdfs上,hadoop集群的部署后续会有一篇文章记录,本文假设had ...

  8. Oracle登录后提示ORA-12154:TNS:无法解析指定的连接标识符

    下午重装系统,安装Oracle,设置了首选项的Oracle主目录名和OCI库,但还是提示ORA-12154:TNS:无法解析指定的连接标识符 纠结了好久,发现刚装系统没设置Oacle数据库的系统环境变 ...

  9. ASP.NET 散碎知识

    1.按钮点击打开一个新的Web窗体,可在按钮点击事件里面写:Response.Redirect("窗体的名字.aspx"); 2.复合控件: CheckBoxList - 复选框组 ...

  10. vue 异步请求

    摘自 ECMAScript 6 简介: 大家习惯将 ECMAScript 6.0 简称为 ES6,它是 Javascript 语言的下一代标准,它的目标,是使得 Javascript 语言可以用来编写 ...