线程的命名操作、线程的休眠、线程优先级
线程的所有操作几乎都在Thread类中定义好了。
线程的命名和取得(重点)
从本质上讲多线程的运行状态并不是固定的。要想确定线程的执行,唯一的区别就在于线程的名称上。在起名时就应该尽可能避免重名,或者避免修改名称。
既然线程的执行本身是不确定的状态,如果要取得线程名字,那么唯一能做的就是取得当前线程的名字。所以在Thread类里面提供有这样的方法:public static Thread currentThread()。
范例:线程的命名和取得
class MyThread implements Runnable{//表示实现多线程
public void run() {//覆写run()方法,线程的主方法
for(int x=0;x<10;x++){
System.out.println(Thread.currentThread().getName()+",x="+x);
}
}
}
public class Hello{
public static void main(String args[]) throws Exception {
MyThread mt = new MyThread();
new Thread(mt,"线程A").start();
new Thread(mt).start();
new Thread(mt).start();
}
}
如果在设置线程对象时没有设置具体的名字,那么就采用一个默认的名字进行定义。
范例:观察以下代码
class MyThread implements Runnable{//表示实现多线程
public void run() {//覆写run()方法,线程的主方法
System.out.println("MyThread线程类:"+Thread.currentThread().getName());
}
}
public class Hello{
public static void main(String args[]) throws Exception {
MyThread mt = new MyThread();
new Thread(mt).start();//线程启动调用run()方法
mt.run();//直接通过对象调用run()方法
}
}
线程依附进程存在,进程呢?
每当使用java命令在JVM上解释某一个程序执行的时候,都会默认启动一个JVM的进程,而主方法只是这个进程的一个线程,所以整个程序一直都跑在线程的运行机制上。
每一个JVM至少会启动两个线程:主线程、GC线程。
线程的休眠
如果要想让某些线程延缓执行,那么就可以使用休眠方式来进行处理,在Thread类里面提供有如下休眠操作:
- 休眠方法:
public static void sleep(long millis, int nanos) throws InterruptedException
如果休眠的时间未到就停止休眠,那么就会产生中断异常。
范例:观察休眠
class MyThread implements Runnable{//表示实现多线程
public void run() {//覆写run()方法,线程的主方法
for(int x=0;x<100;x++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+",x="+x);
}
}
}
public class Hello{
public static void main(String args[]) throws Exception {
MyThread mt = new MyThread();
new Thread(mt,"线程A").start();
new Thread(mt,"线程B").start();
new Thread(mt,"线程C").start();
}
}
以上的代码执行中感觉像是所有的线程对象都同时休眠了,但是严格来讲不是同时,是有先后顺序的,只不过这个顺序小一点而已。
class MyThread implements Runnable{//表示实现多线程
public void run() {//覆写run()方法,线程的主方法
for(int x=0;x<100;x++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+",x="+x);
}
}
}
public class Hello{
public static void main(String args[]) throws Exception {
MyThread mt = new MyThread();
Thread t = new Thread(mt,"线程A");
t.start();
Thread.sleep(2000);
t.interrupt();//中断
}
}
后续会使用休眠来进行线程的分析。
线程的优先级
从理论上讲优先级越高的线程越有可能先执行。而在Thread类里面定义有以下的优先级的操作方法:
- 设置优先级:public final void setPriority(int newPriority)
- 取得优先级:public final int getPriority()
而对于优先级,一共定义有三种: - 最高优先级:public static final int MAX_PRIORITY,10;
- 中等优先级:public static final int NORM_PRIORITY,5;
- 最低优先级:public static final int MIN_PRIORITY,1;
范例:观察优先级
class MyThread implements Runnable{//表示实现多线程
public void run() {//覆写run()方法,线程的主方法
for(int x=0;x<100;x++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+",x="+x);
}
}
}
public class Hello{
public static void main(String args[]) throws Exception {
MyThread mt = new MyThread();
Thread t1 = new Thread(mt,"线程A");
Thread t2 = new Thread(mt,"线程B");
Thread t3 = new Thread(mt,"线程C");
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t3.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}
范例:主线程的优先级是什么呢?
class MyThread implements Runnable{//表示实现多线程
public void run() {//覆写run()方法,线程的主方法
for(int x=0;x<100;x++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+",x="+x);
}
}
}
public class Hello{
public static void main(String args[]) throws Exception {
System.out.println(Thread.currentThread().getPriority());
}
}
可以发现主线程属于一般优先级。