Java学习——线程的礼让、加入、优先级、守护线程
线程的礼让:让出cpu的时间片,但是礼让是否成功还是要看cpu的心情。线程礼让在Java中用到了一个yeild()方法,我们在这里举一个例子,小明和老人一起过马路,小明礼让老人。所以结果是老人先结束小明才开始的纪律会比较大,但也不是说一定是老人小走,如下:
public class ThreadYield{
public static void main(String[] args) {
XiaoMing xiaoMing = new XiaoMing();
OldMan oldMan = new OldMan();
new Thread(xiaoMing,"小明").start();
new Thread(oldMan,"老人").start();
}
}
class XiaoMing implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"启动了");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"结束了");
}
}
class OldMan implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"启动了");
System.out.println(Thread.currentThread().getName()+"结束了");
}
}
结果:
老人启动了
老人结束了
小明启动了
小明结束了
这就说明小明礼让成功了,我们要注意的是线程礼让只是一种概率,不是一定的。
线程加入:当A线程运行到某一处是,让B线程加入,那么A线程必须让B线程运行结束再接着运行,是强制执行的。如下:
public class ThreadJoin{
public static void main(String[] args) throws InterruptedException {
//开启B线程
B b = new B();
Thread t = new Thread(b);
t.start();
for (int i = 0; i < 10; i++) {
//判断i=5时让B线程先运行
if (i==5){
t.join();
}
System.out.println("我是A线程"+i);
}
}
}
class B implements Runnable{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("我是B线程"+i);
}
}
}
输出结果:
我是A线程0
我是A线程1
我是A线程2
我是A线程3
我是A线程4
我是B线程0
我是B线程1
我是B线程2
我是B线程3
我是B线程4
我是A线程5
我是A线程6
我是A线程7
我是A线程8
我是A线程9
我们可以看到,当A线程走到5时,B线程加入进来而且运行完,才继续A线程。这就是线程加入,是强制性的。
设置线程优先级:线程优先级是可以设置的,但是设置后也只是提高了线程运行的概率,并不是一定的。我们来看下如何去设置线程的优先级,线程的优先级有三个常量可以设置:MIN_PRIORITY最小的优先级,为1;MAX_PRIORITY最大的优先级,为10;NORM_PRIORITY默认优先级,不设置都是这个优先级,为5。当然我们还可以设置1-10之间的任何数字,如下:
public class TestPriority {
public static void main(String[] args) {
Thread t1 = new Thread(new ThreadPriority());
Thread t2 = new Thread(new ThreadPriority());
Thread t3 = new Thread(new ThreadPriority());
Thread t4 = new Thread(new ThreadPriority());
Thread t5 = new Thread(new ThreadPriority());
//设置线程优先级
t1.setPriority(2);//
t2.setPriority(8);
t3.setPriority(6);
t4.setPriority(4);
t5.setPriority(9);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
class ThreadPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-------->"+Thread.currentThread().getPriority());
}
}
输出:
Thread-1-------->8
Thread-4-------->9
Thread-2-------->6
Thread-3-------->4
Thread-0-------->2
我们可以看到优先级高的还不一定最先输出,所以说这只是一种概率,并不一定。当然每次输出都可能不一样,多去运行感受。
守护线程:Java分为两种线程:用户线程和守护线程
所谓守护线程是指在程序运行的时候在后台提供一种通用服务的线程,比如垃圾回收线程就是一个很称职的守护者,并且这种线程并不属于程序中不可或缺的部分。因 此,当所有的非守护线程结束时,程序也就终止了,同时会杀死进程中的所有守护线程。反过来说,只要任何非守护线程还在运行,程序就不会终止。
守护线程和用户线程的没啥本质的区别:唯一的不同之处就在于虚拟机的离开:如果用户线程已经全部退出运行了,只剩下守护线程存在了,虚拟机也就退出了。 因为没有了被守护者,守护线程也就没有工作可做了,也就没有继续运行程序的必要了。
将线程转换为守护线程可以通过调用Thread对象的setDaemon(true)方法来实现。在使用守护线程时需要注意一下几点:
(1) thread.setDaemon(true)必须在thread.start()之前设置,否则会跑出一个IllegalThreadStateException异常。你不能把正在运行的常规线程设置为守护线程。
(2) 在Daemon线程中产生的新线程也是Daemon的。
(3) 守护线程应该永远不去访问固有资源,如文件、数据库,因为它会在任何时候甚至在一个操作的中间发生中断。
如下:
/**
* 守护线程
*/
public class TestDaemon {
public static void main(String[] args) {
God god = new God();
Thread thread = new Thread(god);
thread.setDaemon(true);//设置线程为守护线程
thread.start();
Thread thread1 = new Thread(new You1());
thread1.start();
}
}
class You1 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println("--------------------你活了"+i+"天");
}
System.out.println("世界拜拜=======================================");
}
}
class God implements Runnable{
@Override
public void run() {
for (;true;){
System.out.println("上帝保护你");
}
}
}
输出:
上帝保护你
上帝保护你
--------------------你活了0天
--------------------你活了1天
--------------------你活了2天
--------------------你活了3天
--------------------你活了4天
--------------------你活了5天
--------------------你活了6天
--------------------你活了7天
--------------------你活了8天
--------------------你活了9天
--------------------你活了10天
--------------------你活了11天
--------------------你活了12天
--------------------你活了13天
--------------------你活了14天
--------------------你活了15天
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
上帝保护你
--------------------你活了16天
--------------------你活了17天
--------------------你活了18天
--------------------你活了19天
--------------------你活了20天
--------------------你活了21天
--------------------你活了22天
--------------------你活了23天
--------------------你活了24天
--------------------你活了25天
--------------------你活了26天
--------------------你活了27天
--------------------你活了28天
--------------------你活了29天
世界拜拜=======================================