资料来源于网络,仅供参考学习。
 
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. CentOS7 64位 安装MySQL5.7

    安装环境:CentOS7 64位 MINI版,安装MySQL5.7 1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo ...

  2. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  3. windows10 使用gitblit搭建git服务器

    今天在win10上使用gitblit搭建git服务器时被坑了下,因为安装的java9出现不兼容问题,果断卸载重装了jdk8.废话不多说直接进入正题吧: 第一章 前言 使用gitblit搭建git se ...

  4. 使用.Net Core+EF7 完成CodeFirst

    emmm,本来想着用Core做一个小项目玩玩的,然后肯定是要用到数据库的, 然后想,啊,要不用CodeFirst,感觉很腻害的样子,于是,一脸天真无邪的我就踏入了一个深不见底的天坑... 本来想着,应 ...

  5. 多线程编程学习笔记——async和await(一)

    接上文 多线程编程学习笔记——任务并行库(一) 接上文 多线程编程学习笔记——任务并行库(二) 接上文 多线程编程学习笔记——任务并行库(三) 接上文 多线程编程学习笔记——任务并行库(四) 通过前面 ...

  6. python 正则的使用 —— 编写一个简易的计算器

    在 Alex 的博客上看到的对正则这一章节作业是编写一个计算器,要求能计算出下面的算式. 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 + ...

  7. UPDATE/INSERT用法研究

    UPDATE和INSERT语法相信大家都很熟悉,UPDATE的基本语法结构是 : UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值 INSERT的基本语法是: INSERT ...

  8. maven jetty struts异常 There is no Action mapped for namespace [/] and action name [] associated with context path

    毕业设计中用maven jetty插件调试时,struts出现这个错误,直接http://localhost:8080 无法进入默认主页,但换tomcat就没问题,最后在这篇文章找到答案 http:/ ...

  9. google C++编程风格指南之头文件的包括顺序

    google C++编程风格对头文件的包括顺序作出例如以下指示: (1)为了加强可读性和避免隐含依赖,应使用以下的顺序:C标准库.C++标准库.其他库的头文件.你自己project的头文件.只是这里最 ...

  10. Emoji表情符号录入MySQL数据库报错的解决方式

    前言:手机app应用评论的时候,恢复表情符号.提示失败.​1,查看tomcat后台日志,核心报错信息例如以下:  Caused by: java.sql.SQLException: Incorrect ...