---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
守护线程:
setDaemon(boolean on):
使用Thread类中的 static void setDaemon(boolean on)方法当on参数为true时,将线程标记为守护线程
或者用户线程(后台线程)。当正在运行的线程都是守护线程时,java虚拟机退出。程序结束。
注:该方法必须在启动线程前调用。
守护线程的特点是:
随着前台线程结束而结束。
setDaemon实例代码:
package com.itheima.threaddemos.ThreadCommunication;
/**
* 线程停止测试类
* @author wuyong
*
*/
public class SetDaemonDemo {
/**
* @param args
*/
public static void main(String[] args) {
MyStopThread mst = new MyStopThread();
Thread t1= new Thread(mst);
Thread t2 = new Thread(mst);
//调用setDaemon(boolean flag)方法将线程设置为守护线程,即后台线程
// t1.setDaemon(true);
// t2.setDaemon(true);
//线程启动
t1.start();
t2.start();
//主线程执行的代码
for (int j = 0; j <= j ; j++) {
if (j == 60) {
// 强制将冻结状态下的线程恢复到运行状态,使线程对标记进行判断,从而到达控制循环,以结束run方法。
t1.interrupt();
t2.interrupt();
break;
}
System.out.println(Thread.currentThread().getName() + "---run");
}
}
}
/**
* 自定义线程类
* @author wuyong
*
*/
class MyStopThread implements Runnable{
//控制线程状态的标记
boolean flag = true;
@Override
public synchronized void run() {
while (flag) {
try {
this.wait();//线程等待(冻结状态)
} catch (InterruptedException e) {
flag = false;
System.out.println(Thread.currentThread().getName() + "stop");
}
System.out.println(Thread.currentThread().getName() + "...run");
}
}
}
线程的join()方法:
当A线程执行到了B线程等待join等待时,A就会将执行权交给B,等B线程都执行完,A才会执行。
join的作用:可以用来临时加入线程执行,即可以手动的获取CPU执行权。线程的yield() 方法:
作用:暂停当前正在执行的线程对象,并执行其他线程。
join和yield示例代码:
package com.itheima.threaddemos.ThreadCommunication;
/**
* 线程中join方法的测试类
* @author wuyong
*
*/
public class JoinMethodDemo {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
JoinThread jt = new JoinThread();
Thread t1 = new Thread(jt);
Thread t2 = new Thread(jt);
t1.start();
// t1.join();//t1调用join方法,来获得主线程的执行权,此时,主线程处于冻结状态,要等到t1执行完毕时,主线程才会执行。
t2.start();
for (int i = 0; i < 60; i++) {
// System.out.println(Thread.currentThread().getName() + "---" + i);
}
}
}
/**
* 自定义的线程类
* @author wuyong
*
*/
class JoinThread implements Runnable{
@Override
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println(Thread.currentThread().getName() + "..." + i);
Thread.yield();//使当前线程暂停。执行其他线程。
}
}
}
线程的优先级:
线程的优先级范围:1~10;
用Thread类的setPriority(int newPriority)更改线程的优先级。其中Thread类中定义了三个优先级常量:
1,MAX PRIORITY:10;3, NOM PRIORITY:5。
2, MIN PRIORITY:1;
线程默认的优先级为5。作用:提高线程抢夺CPU执行权的频率,但不能保证一定就是哪个优先。
语法:Thread thread = new Thread();
thread.setPriority(3);//设置线程的优先级。---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
join示例代码:
package com.itheima.threaddemos.ThreadCommunication;
/**
* 线程中join方法的测试类
* @author wuyong
*
*/
public class JoinMethodDemo {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
JoinThread jt = new JoinThread();
Thread t1 = new Thread(jt);
Thread t2 = new Thread(jt);
t1.start();
// t1.join();//t1调用join方法,来获得主线程的执行权,此时,主线程处于冻结状态,要等到t1执行完毕时,主线程才会执行。
t2.start();
for (int i = 0; i < 60; i++) {
// System.out.println(Thread.currentThread().getName() + "---" + i);
}
}
}
/**
* 自定义的线程类
* @author wuyong
*
*/
class JoinThread implements Runnable{
@Override
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println(Thread.currentThread().getName() + "..." + i);
Thread.yield();//使当前线程暂停。执行其他线程。
}
}
}