资料来源于网络,仅供参考学习。
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多线程笔记[未更新完]
最近课上可摸鱼时间较多,因此并发开坑学习 本篇学习自Java多线程编程实战指南 目前进展:刚开坑,处于理解概念阶段 本篇学习自Java多线程编程实战指南 Q.进程和线程的区别 进程Process是程序 ...
- Java 多线程 笔记 转自http://www.cnblogs.com/lwbqqyumidi/p/3804883.html
多线程作为Java中很重要的一个知识点, 一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上图中基本上囊括了Java中多线程各重要知识点.掌握了上图中 ...
- Java多线程笔记总结
1.线程的三种创建方式 对比三种方式: 通过继承Thread类实现 通过实现Runnable接口 实现Callable接口 第1种方式无法继承其他类,第2,3种可以继承其他类: 第2,3种方式多线程可 ...
- 这份java多线程笔记,你真得好好看看,我还没见过总结的这么全面的
1.线程,进程和多线程 1.程序:指指令和数据的有序集合,其本身没有任何意义,是一个静态的概念 2.进程:指执行程序的一次执行过程,是一个动态的概念.是系统资源分配的单位(注意:很多多线程是模拟出来的 ...
- java多线程笔记
一,线程的状态 1,新建状态:新创建了一个线程对象 2,就绪状态:线程创建对象后,线程调用star()的方法,等待获取CPU的使用权. 3,运行状态:获取了cpu的使用权,执行程序代码 4,阻塞状态: ...
- 0037 Java学习笔记-多线程-同步代码块、同步方法、同步锁
什么是同步 在上一篇0036 Java学习笔记-多线程-创建线程的三种方式示例代码中,实现Runnable创建多条线程,输出中的结果中会有错误,比如一张票卖了两次,有的票没卖的情况,因为线程对象被多条 ...
- Java学习笔记-多线程-创建线程的方式
创建线程 创建线程的方式: 继承java.lang.Thread 实现java.lang.Runnable接口 所有的线程对象都是Thead及其子类的实例 每个线程完成一定的任务,其实就是一段顺序执行 ...
- Java多线程技术学习笔记(二)
目录: 线程间的通信示例 等待唤醒机制 等待唤醒机制的优化 线程间通信经典问题:多生产者多消费者问题 多生产多消费问题的解决 JDK1.5之后的新加锁方式 多生产多消费问题的新解决办法 sleep和w ...
- java多线程学习笔记——详细
一.线程类 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中, ...
随机推荐
- 访问者模式(Visitor)
访问者模式(Visitor) 访问者模式把数据结构和作用于结构上的操作解耦合,使得操作集合可相对自由地演化.访问者模式适用于数据结构相对稳定算法又易变化的系统.因为访问者模式使得算法操作增加变得容易. ...
- Axios 执行post发送两次请求的小坑
vue-resource2.0已经不再更新,所以vue2.0官方推荐使用axios来代替.实际项目也是应用上了vue+axios,然后就有了这么一段填坑的经历. 问题:axios使用post请求时,发 ...
- Uncaught TypeError: download is not a function at HTMLAnchorElement.onclick (index.html:25)
前段时间调试html报了这样的一个错误 Uncaught TypeError: download is not a function at HTMLAnchorElement.onclick ...
- CCF-201409-3-字符串匹配
问题描述 试题编号: 201409-3 试题名称: 字符串匹配 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给出一个字符串和多行文字,在这些文字中找到字符串出现的那些行.你 ...
- C/C++筛选法算素数
什么是求素数 )i在2到n-1之间任取一个数,如果n能被整除则不是素数,否则就是素数 普通枚举法: #include <iostream> #include <string> ...
- 从Windows迁移SQL Server到Linux
前一篇博客关于SQL Server on Linux的安装,地址:http://www.cnblogs.com/fishparadise/p/8057650.html,现在测试把Windows平台下的 ...
- Effective Java 第三版——1. 考虑使用静态工厂方法替代构造方法
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- NDK开发过程自认为好的一些参考资料
虽然NDK开发时间很短, 但也接触了一些自认为还不错的资料, 记录下来. 一.首先就说官方文档吧 网上资料好多过时了, 并且有点参差不齐. 所以看官方文档还是很有必要的,我根据我的需求整理了两个的链接 ...
- 分布式文件系统及FastDFS
1.前言 今天来谈谈分布式文件系统,侧重点是文件系统,分布式稍微带一下.然后聊下我用的FastDFS的例子. 2.从小需求开始 我的博客的编辑器用的是markdown,它内嵌了一个文件上传功能,不过后 ...
- Excel大写和小写转换函数
Excel中的大写和小写转换函数 (1).转换为所有小写字母:lower函数 (2).转换为所有大写字母:upper函数 (3).转换为首字母大写,其余小写字母:proper函数 三种函数的使用方式. ...