码迷,mamicode.com
首页 > 编程语言 > 详细

Java 多线程笔记

时间:2017-12-29 15:00:08      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:returns   may   any   tween   优先级   笔记   start   无限   code   

资料来源于网络,仅供参考学习。
 
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 多线程笔记

标签:returns   may   any   tween   优先级   笔记   start   无限   code   

原文地址:https://www.cnblogs.com/huangzejun/p/8143828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!