资料来源于网络,仅供参考学习。
 
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. 对java多线程里Synchronized的思考

    Synchronized这个关键字在多线程里经常会出现,哪怕做到架构师级别了,在考虑并发分流时,也经常会用到它.在本文里,将通过一些代码实验来验证它究竟是"锁"什么. 在启动多个线 ...

  2. Redis-Nosql数据库入门

    简介 Redis是Nosql数据库的一种,可基于内存亦可持久化的日志型.是一个Key-Value数据库,多用在缓存方面 安装 Windows 下载地址, 最新版本的Redis好像仅支持64位 Wind ...

  3. Python资料汇总(建议收藏)

    整理汇总,内容包括长期必备.入门教程.练手项目.学习视频. 一.长期必备. 1. StackOverflow,是疑难解答.bug排除必备网站,任何编程问题请第一时间到此网站查找. https://st ...

  4. nginx 配置入门

    之前的nginx配置是对nginx配置文件的具体含义进行讲解,不过对于nginx的新手可能一头雾水. 今天看到个文档不错,翻译过来分享给大家,可以让新手更详细地了解nginx配置,可以说是nginx配 ...

  5. js 深拷贝和浅拷贝

    js 深拷贝和浅拷贝 先举一下项目中遇到的两个例子: 例子1: var json = $.parseJSON(data.data);//data.data是接口返回的值var a = json.cha ...

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

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

  7. onload和ready的区别

    onload和ready的区别 1.执行时间 window.onload必须等到页面内包括图片的所有元素加载完毕后才能执行 $(document).read()是DOM结构绘制完毕后就执行,不必等到加 ...

  8. request和response中文乱码问题后台处理办法

    request接收参数的中文乱码的处理: GET: 方法一:使用String的构造方法: new String(request.getParameter("传过来的name").g ...

  9. linux下分析Java程序内存汇总

    使用pmap查看进程内存 执行命令 使用pmap能够查看某一个进程(非java的也能够)的内存使用使用情况, 命令格式: pmap 进程id 演示样例说明 比如执行: pmap 12358 显示结果例 ...

  10. Ubuntu SSH root 登录 Permission denied 错误

    问题: $ ssh root@40.125.21.75 root@40.125.21.75's password: Permission denied, please try again. 解决方式, ...