java学习——线程的退出,中断,插队以及常用方法

目录

1.线程的操作

1.线程的退出

2.线程的中断

3.线程的插队

2.方法总结:


1.线程的操作

1.线程的退出

线程的退出就是我们在main进程里面控制子线程的退出,根据具体情况来实现,如下:

public class ThreadExit_ {
    public static void main(String[] args) throws InterruptedException {

        T t = new T();
        t.start();
        //我们需要修改T线程的loop
        Thread.sleep(10000);
        t.setLoop(false);
        }
}
class T extends Thread{
    private boolean loop=true;

    public void setLoop(boolean loop) {
        this.loop = loop;
    }

    @Override
    public void run() {
        while (loop) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("T 运行中......");;
        }
    }
}

我们创建一个子线程然后通过loop来控制线程的运行,如果我们要对控制子线程的运行,那么我们需要在main线程中对T中的loop进行改变,这里我们设置一个setLoop方法 ,通过调用setLoop方法去使得进程改变,或者说退出。

2.线程的中断

public class ThreadMethod {
    public static void main(String[] args) throws InterruptedException {
        T4 t4 = new T4();
        t4.setName("tzy");
        t4.setPriority(Thread.MIN_PRIORITY);

        t4.start();//启动子线程
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            System.out.println(" hi  "+i);
        }
        System.out.println("优先级+"+ t4.getPriority());
        t4.interrupt();//中断
    }
}
class T4 extends Thread{

    @Override
    public void run() {
        while (true) {
            for (int i = 0; i < 100; i++) {
                System.out.println("ccc");
            }
            try {
                System.out.println(Thread.currentThread().getName() + "sleeping-----------");
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                //InterruptedException捕获中断异常
                System.out.println(Thread.currentThread().getName() + "inrrupt-----------");

            }
        }
    }
}

对于interrupt方法,我们可以类比循环里面的continue语句,他不结束循环,他只是执行某一次线程的执行。

3.线程的插队

public class ThreadMethodExersice {

    public static void main(String[] args) throws InterruptedException {
        TT3 tt3 = new TT3();
        Thread thread = new Thread(tt3);
        for (int i = 0; i < 10; i++) {
            System.out.println("hi   "+i);
            Thread.sleep(1000);
            if(i==5){
                thread.start();
                thread.join();
            }
        }

    }
}
class TT3 implements Runnable{
    int count=0;
    @Override
    public void run() {

        while (true){
            System.out.println("hello"+(++count));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if(count==10){
                break;
            }
        }
    }
}

对于插队来说,假设你在a线程里面调用b.join()方法这个时候,a线程会进入TimedWaiting状态。在b线程时间结束的时候继续执行a进程。

2.方法总结:

1.setname()//设置线程的名字

2.getname()//获得线程的名字

3.start()//启动线程

4.run()//重写线程执行的代码

5.setPriority()//设置优先级

6.getPriority()//获得优先级

7.sleep()//休眠

8.interrupt()//中断,不结束线程,只是中断某一次线程的执行

9.join()//插队,暂时停止正在执行的线程,执行新的线程。

ps:1.start()底层会调用新的线程,调用run,run就是一个简单的方法调用,不会启动新线程。2.线程优先级的范围。3.interrupt,中断线程没有结束线程。4.sleep:线程的静态方法,使得当前线程休眠。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值