线程的五状态
线程有五种状态:新建态,就绪态,运行态,阻塞态,退出态。
- 新建态:当线程对象被创建时,如:Thread t = new Thread();线程就处于新建态。
- 就绪态:但调用start()方法时,线程就进入就绪态,等待cpu的调度。
- 运行态:cpu调度线程,线程开始运行。
- 阻塞态:当调用wait(),sleep()方法或同步锁定时,线程就进入阻塞态,不再往下执行,直到阻塞事件解锁,重新进入就绪态。
- 退出态:线程执行完毕或中断。
停止线程
//1.建议线程正常停止-》利用次数,不建议死循环
//2.建议使用标志位-》设置一个标志位
//3.不要使用stop或者destroy等过时或jdk不建议使用的方法
public class testThreadStop implements Runnable{
//设置一个标志位
private boolean flag = true;
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while(flag) {
System.out.println("run...Thread"+i++);
}
}
//设置一个公开的方法停止线程,转换标志位
public void stop() {
this.flag = false;
}
public static void main(String[] args) {
testThreadStop testStop = new testThreadStop();
new Thread(testStop).start();
for(int i = 0; i < 1000; i++) {
System.out.println("main"+i);
if(i==900) {
//调用stop方法切换标志位,停止线程
testStop.stop();
System.out.println("线程停止");
}
}
}
}
线程休眠
调用sleep(时间)方法,参数为指定当前线程阻塞的毫秒数,存在异常InterruptedException。
//模拟网络延时:放大问题的发生性
public class tsestSleep implements Runnable {
//票数
private int ticketNums = 10;
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
if(ticketNums<=0) {
break;
}
//模拟延时
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-->拿到了第"+ticketNums--+"票");
}
}
public static void main(String[] args) {
tsestSleep ticket = new tsestSleep();
new Thread(ticket,"小明").start();
new Thread(ticket,"老师").start();
new Thread(ticket,"黄牛").start();
}
}
线程礼让
让当前正在执行的线程暂停,进入就绪态,cpu重新调度。礼让不一定成功。
//测试礼让线程
//礼让不一定成功
public class testYield {
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield,"a").start();
new Thread(myYield,"b").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName()+"线程开始执行 ");
Thread.yield();//礼让
System.out.println(Thread.currentThread().getName()+"线程停止 ");
}
}
线程合并
带当前线程执行完毕后,再执行其他线程,其他线程阻塞。
//测试join方法。。。插队
public class testJoin implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 200; i++) {
System.out.println("线程vip来了"+i);
}
}
public static void main(String[] args) throws InterruptedException {
//启动线程
testJoin tjoin = new testJoin();
Thread thread = new Thread(tjoin);
thread.start();
//主线程
for(int i = 0; i < 1000; i++) {
if(i==200) {
thread.join();
}
System.out.println("main"+i);
}
}
}
线程状态观察
//观察测试线程状态
public class testState {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
for(int i=0;i<5;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("//////");
});
//观察状态
Thread.State state = thread.getState();
System.out.println(state);//new
//观察启动后
thread.start();//启动线程
state = thread.getState();
System.out.println(state);//Run
while(state != Thread.State.TERMINATED) {
//只要线程不终止,就一直输出
Thread.sleep(100);
state = thread.getState();//更新状态
System.out.println(state);//输出状态
}
}
}
线程的优先级
调用setPriority()设置线程的优先级,10为最高,1最低。
//测试线程的优先级
public class testPriority {
public static void main(String[] args) {
//主线程默认优先级
System.out.println(Thread.currentThread().getName()+"->"+Thread.currentThread().getPriority());
MyPriority myPriority = new MyPriority();
Thread t1 = new Thread(myPriority);
Thread t2 = new Thread(myPriority);
Thread t3 = new Thread(myPriority);
Thread t4 = new Thread(myPriority);
Thread t5 = new Thread(myPriority);
Thread t6 = new Thread(myPriority);
//先设置优先级再启动
t1.start();
t2.setPriority(1);
t2.start();
t3.setPriority(4);
t3.start();
t4.setPriority(Thread.MAX_PRIORITY);
t4.start();
t5.setPriority(8);
t5.start();
t6.setPriority(7);
t6.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName()+"->"+Thread.currentThread().getPriority());
}
}
守护线程
线程分为用户线程和守护线程,虚拟机必须确保用户线程执行完毕,但不用等待守护线程执行完毕。
//测试守护线程
public class testDaemon {
public static void main(String[] args) {
God god = new God();
You you = new You();
Thread thread = new Thread(god);
thread.setDaemon(true);//默认是false,即为用户线程
thread.start();
new Thread(you).start();//用户线程,用户线程执行完毕,守护线程也停止
}
}
class God implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("上帝保佑你");
}
}
class You implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 36500; i++) {
System.out.println("你一生都开心的活着");
}
System.out.println("===goodbye! world!===");
}
}