Java多线程基础,线程的创建使用以及终止

我们在使用多任务操作系统时,比如windows,可以一边聊着qq,一边听着音乐,一边玩着游戏等等,这一个个的程序就是一个个进程,而每个进程里至少有一个线程在执行任务,比如,qq,有个线程在执行文件下载,有个线程执行发送消息,这些线程在并行的执行着,互不影响,这就是多线程技术,多线程技术的使用,提高了cpu的利用率,提高了进程的工作效率。下面我们看看java中是怎么使用多线程技术的。

一、线程的创建和使用

有两种方式创建多线程,分别如下

1.继承Thread类,覆写run方法

public class MyThread extends Thread {
    @Override
    public void run() {
        super.run();
        System.out.println(Thread.currentThread().getName());
    }
}

场景类使用如下,调用thread的start方法即可开启运行一个子线程

public class Test {
    public static void main(String[] args){
        MyThread myThread = new MyThread();
        myThread.start();
    }
}

打印结果:Thread-0

2.实现Runnable接口

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("id="+Thread.currentThread().getId()+" name="+Thread.currentThread().getName());
    }
}

场景类使用如下:

public class Test {
    public static void main(String[] args){
       new Thread(new Runnable() {
            @Override
            public void run() {
                    System.out.println("id="+Thread.currentThread().getId()+" name="+Thread.currentThread().getName());
            }
        }).start();
    }
}

此外,我们看一下Thread类的定义:

public class Thread implements Runnable

发现,Thread 类本身也实现了Runnable接口,那么你想到了什么,
就是,我们在创建Thread类的时候,也可以使用Thread类的对象作为参数传进来使用,代码如下:

public class Test {
    public static void main(String[] args){
        Thread thread = new Thread(new MyThread());
        thread.start();
    }
}

小结:这两种方式的区别是:因为java只支持单继承,所以如果一个类已经继承了其他类,那么只能使用实现Runnable接口这种方式了。

二、停止线程:

2.1判断线程是否是停止状态

怎么判断线程是否是停止状态,看Thread的两个方法:

public static native boolean interrupted();
静态方法,判断当前线程是否处于中断状态,(若处于中断状态返回true,并清除中断状态(即下次再调用这个方法则返回false),)
比如

public class Test {
    public static void main(String[] args){
       Thread thread = new Thread(new MyThread());
        thread.interrupted();
    }
}

这句代码并不是判断thread对象是否终止,而是判断Thread.currentThread这个对象的线程是否终止,即main线程。并清除线程的中断状态。

public native boolean isInterrupted();
这个是判断调用该方法的Thread类的对象是否终止,比如下面这段代码,则就是判断thread对象是否是处于中断状态。

public class Test {
    public static void main(String[] args){
       Thread thread = new Thread(new MyThread());
        thread.isInterrupted();
    }
}

我们可以这样记忆:第一个方法是静态的,说明与实例对象无关,那就是指Thread.currentThread这个对象了,而第二个方法不是静态的,所以与实例对象有关,

另外,public void interrupt()这个方法,是让线程具有中断状态,并不能停止线程的运行。(当然如果线程调用了sleep、wait或者join方法,此时再调用interrupt方法,那么线程会抛InterruptedException异常)

2.2停止线程的三个方法:
1.抛异常法:

public class MyThread extends Thread {
    @Override
    public void run() {
        super.run();
        try {
            for (int i = 0; i <500000 ; i++) {

                if (this.isInterrupted()){
                    System.out.println("线程终止");
                    throw new InterruptedException("线程停止了,退出");
                }
                System.out.println("i="+(i+1));
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

场景类:

public class Test {
    public static void main(String[] args){
        MyThread thread = new MyThread();
        thread.start();
        try {
            Thread.sleep(10);
            thread.interrupt();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

2.使用return停止

public class MyThread extends Thread {
    @Override
    public void run() {
        super.run();
            for (int i = 0; i <500000 ; i++) {
                if (this.isInterrupted()){
                    System.out.println("线程终止");
                    return;
                }
                System.out.println("i="+(i+1));
            }

    }
}

3.使用flag停止

public class Test {

   static boolean flag = true;
    public static void main(String[] args){

        Thread thread = new NewThread();
        thread.start();
        try {
            Thread.currentThread().sleep(20);
            flag = false;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
   static class NewThread extends Thread{
        @Override
        public void run() {
            for (int i = 0; i < 500000; i++) {
                if (!flag){
                    System.out.println("线程终止");
                    return;}
                System.out.println("i="+(i+1));
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值