目录
一、引言
在jdk1.5之前多线程有很多基础的功能,下面主要介绍一下最基本的特性,知道会使用就行。
二、优先级
每一个 Java 线程都有一个优先级,这样有助于操作系统确定线程的调度顺序。
Java 线程的优先级是一个整数,其取值范围是 1 (Thread.MIN_PRIORITY ) - 10 (Thread.MAX_PRIORITY )。默认情况下,每一个线程都会分配一个优先级 NORM_PRIORITY(5)。
优先级只是说概率上会大一些,属于模糊执行。
public class Testpriority {
public static void main(String [] args){
Runnable runnable=new Runnable() {
public void run() {
while(true)
{
System.out.println("线程——"+ Thread.currentThread().getName());
}
}
};
Thread thread1= new Thread(runnable);
Thread thread2= new Thread(runnable);
//priority1——10,默认为5
thread1.setPriority(10);
thread2.setPriority(1);
thread1.start();
thread2.start();
}
}
三、睡眠sleep
Thread.sleep(),会使当前线程进行睡眠,转让cpu执行权到其他线程,可以设置睡眠时间。属于确定执行
public class TestSleep {
public static void main(String [] args){
Runnable runnable=new Runnable() {
public void run() {
while(true)
{
try {
Thread.sleep(1000);//该线程休息1秒钟
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程——"+ Thread.currentThread().getName());
}
}
};
new Thread(runnable).start();
}
}
四、加入线程join
在一个线程中调用other.join(),将等待other执行完后才继续本线程。属于确定执行
public class Testjoin {
public static void main(String [] args){
System.out.println(Thread.currentThread().getName()+"主线程启动");
final Thread thread1= new Thread(new Runnable() {
public void run() {
for (int i=0;i<1000;i++)
{
System.out.println("线程——"+ Thread.currentThread().getName());
}
}
});
thread1.start();
Thread thread2= new Thread(new Runnable() {
public void run() {
try {
System.out.println("线程——"+ Thread.currentThread().getName()+"启动");
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i=0;i<1000;i++)
{
System.out.println("线程——"+ Thread.currentThread().getName());
}
System.out.println("线程——"+ Thread.currentThread().getName()+"结束");
}
});
thread2.start();
/* try {
thread1.join();//加入主线程
} catch (InterruptedException e) {
e.printStackTrace();
}*/
System.out.println(Thread.currentThread().getName()+"主线程结束");
}
}
thread2要在 thread1.执行完毕后在thread1.join()处继续执行。
五、礼让线程yield
暂停当前正在执行的线程对象,并执行其他线程。
yield()应该做的是让当前运行线程回到可运行状态,以允许具有相同优先级的其他线程获得运行机会。因此,使用yield()的目的是让相同优先级的线程之间能适当的轮转执行。但是,实际中无法保证yield()达到让步目的,因为让步的线程还有可能被线程调度程序再次选中。属于模糊执行
public class TestYeild {
public static void main(String [] args){
new Thread(new Runnable() {
public void run() {
System.out.println("线程——"+ Thread.currentThread().getName()+"开始");
for (int i=0;i<100;i++)
{
System.out.println("线程——"+ Thread.currentThread().getName()+"礼让");
Thread.yield();//一定程度上让其他线程占用cpu高一些
System.out.println("线程——"+ Thread.currentThread().getName()+"——"+i);
}
System.out.println("线程——"+ Thread.currentThread().getName()+"结束");
}
}).start();
new Thread(new Runnable() {
public void run() {
System.out.println("线程——"+ Thread.currentThread().getName()+"开始");
for (int i=0;i<100;i++)
{
Thread.yield();
System.out.println("线程——"+ Thread.currentThread().getName()+"——"+i);
}
System.out.println("线程——"+ Thread.currentThread().getName()+"结束");
}
}).start();
}
}
只是尽量让thread1和thread2交替执行。
六、守护线程daemon
指为其他线程提供服务的线程,也称为守护线程。JVM的垃圾回收线程就是一个后台线程。用户线程和守护线程的区别在于,是否等待主线程依赖于主线程结束而结束。属于确定执行
public class Testdaemon {
public static void main(String [] args) {
Thread thread=new Thread(new Runnable() {
public void run() {
while (true)
{
System.out.println(Thread.currentThread().getName()+"running");
}
}
});
System.out.println(Thread.currentThread().getName()+"开始");
//thread.setDaemon(true);
thread.start();
System.out.println(Thread.currentThread().getName()+"结束");
}
}
七、中断线程
interrupt中断线程进入死亡状态。属于确定执行
public class Testinterrupt {
public static void main(String [] args) {
Thread thread= new Thread(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName()+"start");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("线程中止");//线程终止后报异常
}
System.out.println(Thread.currentThread().getName()+"end");
}
});
System.out.println(Thread.currentThread().getName()+"start");
thread.start();
try {
Thread.sleep(500);
thread.interrupt();//中止线程报异常,如果使用stop(),子线程与后面的代码不会执行
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"end");
}
}
这里直接抛出异常子线程可以继续执行,使用stop就不能接到异常。
八、总结
- priority;
- sleep;
- join;
- yield;
- daemon;
- interrupt;