线程的休眠、线程的优先级
1.1 线程的休眠
如果要想要让某些线程延缓执行,那么就可以使用使用休眠的方法来进行处理,在Thread类里面提供如下休眠操作:
休眠方法:public static void sleep(long millis) throws InterruptedException,如果休眠时间没有到就停止了休眠,那么就会产生中断异常。
范例:观察休眠
package day1;
class MyThread1 implements Runnable{//表示实现多线程
public void run() {//覆写run(),线程的主方法
for(int i = 0 ;i <100;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ".i = "+ i);
}
}
}
public class TestRunnableDemo {
public static void main(String[] args) {
MyThread1 myThread = new MyThread1();
new Thread(myThread,"线程A").start();//线程启动调用run()方法
new Thread(myThread,"线程B").start();//线程启动调用run()方法
new Thread(myThread,"线程C").start();//线程启动调用run()方法
}
}
线程C.i = 0
线程A.i = 0
线程B.i = 0
线程B.i = 1
线程A.i = 1
线程C.i = 1
线程C.i = 2
线程A.i = 2
线程B.i = 2
线程C.i = 3
线程A.i = 3
线程B.i = 3
。。。。。
以上代码执行中感觉像是所有的线程对象都同事休眠了,但是严格来讲不是同时,是有先后顺序的,只不过这个顺序小一点而已。
package day1;
class MyThread1 implements Runnable{//表示实现多线程
public void run() {//覆写run(),线程的主方法
for(int i = 0 ;i <100;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ".i = "+ i);
}
}
}
public class TestRunnableDemo {
public static void main(String[] args) throws Exception {
MyThread1 myThread = new MyThread1();
Thread t = new Thread(myThread,"线程A");//线程启动调用run()方法
t.start();
Thread.sleep(2000);
t.interrupt();//中断
}
}
线程被中断一定是有其他线程所打断。
后续会使用休眠来进行线程的分析。
1.2 线程的优先级
从理论上讲,优先级越高的线程越有可能先执行,而在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
范例:观察优先级
package day1;
class MyThread1 implements Runnable{//表示实现多线程
public void run() {//覆写run(),线程的主方法
for(int i = 0 ;i <100;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ".i = "+ i);
}
}
}
public class TestRunnableDemo {
public static void main(String[] args) throws Exception {
MyThread1 myThread = new MyThread1();
Thread t1 = new Thread(myThread,"线程A");//线程启动调用run()方法
Thread t2 = new Thread(myThread,"线程B");//线程启动调用run()方法
Thread t3 = new Thread(myThread,"线程C");//线程启动调用run()方法
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t3.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}
线程A.i = 0
线程C.i = 0
线程B.i = 0
线程A.i = 1
线程B.i = 1
线程C.i = 1
线程A.i = 2
线程B.i = 2
线程C.i = 2
线程A.i = 3
线程C.i = 3
线程B.i = 3
...........
范例:主线程的优先级
package day1;
class MyThread1 implements Runnable{//表示实现多线程
public void run() {//覆写run(),线程的主方法
for(int i = 0 ;i <100;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ".i = "+ i);
}
}
}
public class TestRunnableDemo {
public static void main(String[] args) throws Exception {
System.out.println("主线程优先级:" + Thread.currentThread().getPriority());
}
}
主线程优先级:5
可以发现主线程属于一般优先级。
总结:
1.线程要有名字,Thread.currentThread()取得当前线程;
2.线程的休眠是有先后顺序的;
3.理论上线程的优先级越高的越有可能先执行