Java中线程的退出,常用方法以及守护进程和工作进程

进程的退出:

代码:

public class ThreadExit {
    public static void main(String[] args) throws InterruptedException {
        Tag tag = new Tag();
        new Thread(tag).start();
        Thread.sleep(10*1000);//main线程休眠10s
        tag.setLoop(false);//让线程Thread-0退出
    }
}

class Tag implements Runnable{
    private Boolean loop = true;

    public Boolean getLoop() {
        return loop;
    }

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

    @Override
    public void run() {//这个run()并非是Thread类中的方法,而是Thread类实现接口Runnable的方法:public abstract void run();
        while (loop){
            System.out.println("冲啊"+Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

常用方法:

  1. setName()设置线程的名字
  2. setPriority()设置线程的优先级
  3. start()开始线程
  4. interrupt()中断线程
  5. getName()得到线程的名字
  6. join()线程插队,是要插队的线程的实例对象来调用它
  7. yeild()线程礼让,是该线程来调用它,如Thread.yeild();

代码:

public class ThreadMethods1 {
    public static void main(String[] args) throws InterruptedException {
        Tag tag = new Tag();
        Thread thread = new Thread(tag);
        thread.setName("ret");//setName()设置线程的名字
        thread.setPriority(Thread.MIN_PRIORITY);//setPriority()设置线程的优先级MIN_PRIORITY为1
        thread.start();//start()开始线程
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + "正在学JavaWeb");
        }
        thread.interrupt();//interrupt()中断线程。这里执行完后main线程就结束了,后面不会在中断ret线程了
    }
}

class Tag implements Runnable {

    @Override
    public void run() {
        while (true) {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + "正在学Java。 优先级为"+Thread.currentThread().getPriority());//getName()得到线程的名字
            }
            try {
                System.out.println(Thread.currentThread().getName()+"休眠中");
                Thread.sleep(20 * 1000);
            } catch (InterruptedException e) {
                System.out.println("你已经被中断了,继续~");
            }
        }
    }
}
public class ThreadMethods2 {
    public static void main(String[] args) throws InterruptedException {
        Person person = new Person();
        Thread thread = new Thread(person);
        thread.setName("ret");
        thread.start();
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "正在学JavaWeb");
            Thread.sleep(1000);
            if (i == 5) {
                System.out.println(thread.getName() + "先学");
               thread.join();//join()线程插队,让线程名为thread.getName()的先执行完,即线程Person
                //Thread.yield();//yield()线程礼让,让其余线程先执行
            }
        }

    }
}

class Person implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "正在学Java");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

 工作线程(用户线程):当线程的任务执行完或通知方式结束

守护线程:一般是为工作线程服务的,当所有的工作线程结束,守护线程自动结束

常见的守护线程:垃圾回收机制

代码:

实例对象.setDaemon(true);//将线程设置为守护线程

public class Daemon {
    public static void main(String[] args) throws InterruptedException {
        Tag tag = new Tag();
        Thread thread = new Thread(tag);
        thread.setDaemon(true);//将线程Tag设置为守护线程
        thread.start();
        for (int i = 0; i < 5; i++) {
            System.out.println("ret");
            Thread.sleep(1000);
        }
    }
}

class Tag implements Runnable{
    @Override
    public void run() {//这个run()并非是Thread类中的方法,而是Thread类实现接口Runnable的方法:public abstract void run();
        while (true){
            System.out.println("冲啊"+Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值