资料来源于网络,仅供参考学习。
 
1、A Java program ends when all its threads finish (more specifically, when all its non-daemon threads finish). If the initial thread (the one that executes the main() method) ends, the rest of the threads will continue with their execution until they finish. If one of the threads use the System.exit() instruction to end the execution of the program, all the threads end their execution.
译:当所有的线程执行完的时候,Java程序结束(更确切地说,是所有非守护线程执行完的时候)。当初始线程(执行main方法的线程)结束的时,其他线程仍然会执行知道执行结束。但是,如果有一个线程执行了System.exit()这个方法,所有的线程都将结束。
 
2、Creating an object of the Thread class doesn't create a new execution thread. Also, calling the run() method of a class that implements the Runnable interface doesn't create a new execution thread. Only calling the start() method creates a new execution thread.
译:创建Thread类的对象不会创建一个新的执行线程。同样,调用实现Runnable接口的类的run方法也不会创建新的执行线程。只有调用了start方法才会创建新的执行线程。
 
3、The Thread class saves some information attributes that can help us to identify a thread,know its status, or control its priority. These attributes are:
#ID: This attribute stores a unique identifier for each Thread.
#Name: This attribute store the name of Thread.
#Priority: This attribute stores the priority of the Thread objects. Threads can have a priority between one and 10, where one is the lowest priority and 10 is the  highest one. It's not recommended to change the priority of the threads, but it's a possibility that you can use if you want.
#Status: This attribute stores the status of Thread. In Java, Thread can be in one of these six states: new, runnable, blocked, waiting, time waiting,or terminated.
译:Thread类保存了一些特性信息,可以帮助我们识别一个线程,了解一个线程的状态,控制其优先级别。这些特性是:
#ID:这个特性存储了每一个线程的唯一标志。
#NAME:这个特性保存了线程的名称。
#Priority:这个特性保存了线程的优先级别。线程的优先级从0到10,0是最低优先级,10是最高优先级。不推荐改变线程优先级,但是如果你需要使用的话,也可以使用。
#Status:这个特性保存了线程的状态。Java中线程可以处于下列六个状态之一:new、runnable、blocked、waiting、time waiting、teminated.
 
注意:Thread类并没有提供setId、setState这两个方法。
 
4、Another possibility is to use the sleep() method of an element of the TimeUnit enumeration. This method uses the sleep() method of the Thread class to put the current thread to sleep, but it receives the parameter in the unit that it represents and converts it to milliseconds.
译:另一种可以使用sleep的方法是使用TimeUnit的枚举元素。这种方法使用Thread的sleep方法让现在处于执行状态的线程休眠,接收一个参数代表指定的单位,然后把它转化成相应的毫秒数。
 
5、Waiting for the finalization of a thread
In some situations, we will have to wait for the finalization of a thread. For example, we may have a program that will begin initializing the resources it needs before proceeding with the rest of the execution. We can run the initialization tasks as threads and wait for its finalization before continuing with the rest of the program. For this purpose, we can use the join() method of the Thread class. When we call this method using a thread object, it suspends the execution of the calling thread until the object called finishes its execution.
译:等待一个线程的终结
有些情形下,我们必须等待一个线程的终结。例如,我们有一个应用程序在继续完成后续操作前需要对其需要的一些资源进行初始化。我们可以把初始化的任务交给一个线程去做。在执行后续操作前等待这个线程的终结。为了这个目标,我们可以使用Thread的join方法。当我们使用Thread的对象调用这个方法时,调用加入线程的join方法的线程将会暂停,直到加入线程执行完毕才会执行。
 
6、Java provides two additional forms of the join() method:
  join (long milliseconds)
  join (long milliseconds, long nanos)
 
In the first version of the join() method, instead of waiting indefinitely for the finalization of the thread called, the calling thread waits for the milliseconds specified as a parameter of the method. For example, if the object thread1 has the code, thread2.join(1000), the thread thread1 suspends its execution until one of these two conditions is true:
 thread2 finishes its execution 
1000 milliseconds have been passed
When one of these two conditions is true, the join() method returns.The second version of the join() method is similar to the first one, but receives the number
of milliseconds and the number of nanoseconds as parameters.
译:Java提供另外两种形式的join方法:
join (long milliseconds)
 join (long milliseconds, long nanos)
第一个版本的join方法,调用线程不是无限等待加入的线程,而是方法参数指定的毫秒数。例如:如果对象thread1调用的这样的代码--thread2.join(1000),这个时候thread1就会中止执行,知道下面两个条件中的一个条件是true:
thread2执行结束;
1000毫秒已经流逝
当两者条件之一满足时,join方法返回。
第二个版本的join方法与第一个版本类似,它接收毫秒数和nono描述作为参数。
 
7、Creating and running a daemon thread .
Java has a special kind of thread called daemon thread. These kind of threads have very low priority and normally only executes when no other thread of the same program is running.When daemon threads are the only threads running in a program, the JVM ends the program finishing these threads. With these characteristics, the daemon threads are normally used as service providers for normal (also called user) threads running in the same program. They usually have an infinite loop that waits for the service request or performs the tasks of the thread. They can't do important jobs because we don't know when they are going to have CPU time and they can finish any time if there aren't any other threads running. A typical example of these kind of threads is the Java garbage collector.
译:创建并运行一个守护线程
Java有一种特殊的线程,叫做:守护线程。这种线程的优先级别比较低,通常只有在同一个应用程序中其他线程不执行的时候才会执行。当一个程序中只有守护线程在运行的时候,JVM在执行完这些守护线程就会中止程序的运行。基于这些特征,守护线程通常被运行在程序中的其他线程用作服务提供者。他们通常有一个无限循环,以等待服务请求或者执行线程任务。他们通常不做重要的事,因为我们不知道他们何时可以拥有CPU时间并且如果没有其他线程执行的话他们可能随时结束。这种线程的一个经典例子是:Java的垃圾回收器。
 
 
 

Java 多线程笔记的更多相关文章

  1. Java多线程笔记[未更新完]

    最近课上可摸鱼时间较多,因此并发开坑学习 本篇学习自Java多线程编程实战指南 目前进展:刚开坑,处于理解概念阶段 本篇学习自Java多线程编程实战指南 Q.进程和线程的区别 进程Process是程序 ...

  2. Java 多线程 笔记 转自http://www.cnblogs.com/lwbqqyumidi/p/3804883.html

    多线程作为Java中很重要的一个知识点, 一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上图中基本上囊括了Java中多线程各重要知识点.掌握了上图中 ...

  3. Java多线程笔记总结

    1.线程的三种创建方式 对比三种方式: 通过继承Thread类实现 通过实现Runnable接口 实现Callable接口 第1种方式无法继承其他类,第2,3种可以继承其他类: 第2,3种方式多线程可 ...

  4. 这份java多线程笔记,你真得好好看看,我还没见过总结的这么全面的

    1.线程,进程和多线程 1.程序:指指令和数据的有序集合,其本身没有任何意义,是一个静态的概念 2.进程:指执行程序的一次执行过程,是一个动态的概念.是系统资源分配的单位(注意:很多多线程是模拟出来的 ...

  5. java多线程笔记

    一,线程的状态 1,新建状态:新创建了一个线程对象 2,就绪状态:线程创建对象后,线程调用star()的方法,等待获取CPU的使用权. 3,运行状态:获取了cpu的使用权,执行程序代码 4,阻塞状态: ...

  6. 0037 Java学习笔记-多线程-同步代码块、同步方法、同步锁

    什么是同步 在上一篇0036 Java学习笔记-多线程-创建线程的三种方式示例代码中,实现Runnable创建多条线程,输出中的结果中会有错误,比如一张票卖了两次,有的票没卖的情况,因为线程对象被多条 ...

  7. Java学习笔记-多线程-创建线程的方式

    创建线程 创建线程的方式: 继承java.lang.Thread 实现java.lang.Runnable接口 所有的线程对象都是Thead及其子类的实例 每个线程完成一定的任务,其实就是一段顺序执行 ...

  8. Java多线程技术学习笔记(二)

    目录: 线程间的通信示例 等待唤醒机制 等待唤醒机制的优化 线程间通信经典问题:多生产者多消费者问题 多生产多消费问题的解决 JDK1.5之后的新加锁方式 多生产多消费问题的新解决办法 sleep和w ...

  9. java多线程学习笔记——详细

    一.线程类  1.新建状态(New):新创建了一个线程对象.        2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中, ...

随机推荐

  1. Android 软键盘的显示和隐藏,这样操作就对了

    一.前言 如果有需要用到输入的地方,通常会有需要自动弹出或者收起软键盘的需求.开篇明义,本文会讲讲弹出和收起软键盘的一些细节,最终还会从源码进行分析. 想要操作软键盘,需要使用到 InputMetho ...

  2. vue2.x利用脚手架快速构建项目并引入bootstrap、jquery

    要使用vue-cli脚手架搭建项目,首先需要安装node.js Node.js官网:https://nodejs.org/en/download/ 选择你对应的系统即可下载,下载完成后傻瓜式安装即可. ...

  3. 【javaFX学习】(二) 控件手册

    这里写的控件可能不是所有的控件,但是应该是比较齐全并足够用的了,后面还有图表类的,3d模型类,放在后面来写吧,太多了.javafx的功能比以前想象中的要强大.而且也很方便,所有的控件写完后再用Scen ...

  4. Logistic 回归模型 第一遍阅读笔记

    MLE :最大似然估计,求得的这套参数估计能够通过指定模型以最大概率在线样本观测数据 必须来自随机样本,自变量与因变量之间是线性关系 logistic 回归没有关于自变量分布的假设条件,自变量可以连续 ...

  5. CNN网络介绍与实践:王者荣耀英雄图片识别

    欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者介绍:高成才,腾讯Android开发工程师,2016.4月校招加入腾讯,主要负责企鹅电竞推流SDK.企鹅电竞APP的功能开发和技术优化工作 ...

  6. ##4.Glance 镜像服务-- openstack pike

    ##4.Glance 镜像服务 openstack pike 安装 目录汇总 http://www.cnblogs.com/elvi/p/7613861.html ##.Glance 镜像服务.txt ...

  7. C#Winform设计的通用标签设计器

    技术看点 PropertyGrid的使用 自定义控件的使用 对象序列化成XML GDI+Windows驱动打印 前言 是的,一不小心把公司名称透露了.索性帮公司打一下广告.公司(上海易溯信息科技)是中 ...

  8. PHP大文件分割上传(分片上传)

    服务端为什么不能直接传大文件?跟php.ini里面的几个配置有关 upload_max_filesize = 2M //PHP最大能接受的文件大小 post_max_size = 8M //PHP能收 ...

  9. Python操作 Memcache、Redis、RabbitMQ

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  10. 大数据学习系列之二 ----- HBase环境搭建(单机)

    引言 在上一篇中搭建了Hadoop的单机环境,这一篇则搭建HBase的单机环境 环境准备 1,服务器选择 阿里云服务器:入门型(按量付费) 操作系统:linux CentOS 6.8 Cpu:1核 内 ...