温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中Thread类怎么使用和它的属性是什么

发布时间:2022-04-12 17:27:58 来源:亿速云 阅读:123 作者:zzz 栏目:开发技术

本篇内容主要讲解“Java中Thread类怎么使用和它的属性是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java中Thread类怎么使用和它的属性是什么”吧!

在java中可以进行多线程编程,在java标准库中提供了一个Thread类,来表示线程操作。Thread类可以视为java标准库提供的一组解决多线程编程的一组API.

创建好的Thread实例,和操作系统中的线程是一一对应的。操作系统提供了关于线程的API(C语言风格),java在对其进行封装就成Thread类。

创建线程

方法一:继承Thread类

class MyThread extends Thread{
    @Override
    public void run() {
        //此时只是定义处理一个线程类,在系统中总是还没有创建出 新的线程。
        System.out.println("hello thread");
    }
}
public class TestDemo11 {
    public static void main(String[] args) {
        //创建线程
        Thread t = new MyThread();
        //启动线程,在系统中创建出了新的线程
        t.start();
    }
}

Java中Thread类怎么使用和它的属性是什么

线程之间是并发执行的

class MyThread3 extends Thread{
    @Override
    public void run() { //定义一个线程类
        while (true) {
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
public class TestDemo13 {
    public static void main(String[] args) {
        Thread t = new MyThread3();
        t.start();//启动t线程
        while(true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Java中Thread类怎么使用和它的属性是什么

Java中Thread类怎么使用和它的属性是什么

我们可以看到1s当执行一个线程中的代码之后 进入阻塞状态,那么下一秒要唤醒那个线程呢?

我们可以看到执行出来的两个线程中打印出来的日志的顺序是不确定的。每一轮,1s之后,到底是唤醒thread线程还是唤醒main线程,这是不确定的。(抢占式执行),对于操作系统来说,从宏观上对线程进行调度的顺序是随机的

此处在说明一下Thread.sleep()方法,sleep()这个方法到了ms级别没有那么精确。当调用这个方法之后,把线程强制处于中阻塞(睡眠状态),但是当阻塞时间结束之后,并不是立即在cup上继续执行该线程,如果Thread.sleep(1000),当通过1s后,阻塞时间结束,但是在1001ms,线程也许不会立即执行。也许操作系统中的cup在忙别的线程。或许该线程在1006ms才执行。

方法二:实现Runnable接口中的run()方法

//Runnable其实就是描述一个任务
//创建一个类,实现Runnable接口,重写Runnable类中的run方法,在run()方法中,描述的是该线程要指向的哪些任务。
class MyThread2 implements Runnable{
    @Override
    public void run() {
        System.out.println("hello thread");
    }
}
public class TestDemo12 {
    public static void main(String[] args) {
        //创建线程,把创建好的Runnable实例传给Thread实例
        Thread t = new Thread(new MyThread2());
        t.start();
    }
}

方法三:利用内部类

方法三其实是方法一个的翻版,就是把上面的两种代码,改成了匿名内部类。

public class TestDemo14 {
    public static void main(String[] args) {
        Thread t1 = new MyThread(){
            @Override
            public void run() {
                System.out.println("hello thread1");
            }
        };
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello thread2");
            }
        });
        t1.start();
        t2.start();
    }
}

Java中Thread类怎么使用和它的属性是什么

方法四:使用lambmd表达式

public class TestDemo15 {
    public static void main(String[] args) {
        //其实就是使用lambad表达式代替Runnable
        Thread t1 = new Thread(()->{
            //()表示无参的run()方法
            System.out.println("hello thread1");
        });

    }
}

使用线程的好处

为了更方便的体现出多线程的好处,在这里我们从0开始自增1,一直自增到10_0000_0000,使用串行方法,和并行方法,并且获取他们执行代码的时间,进行比较。

public class TestDemo16 {
    public static void func1() throws InterruptedException {
        long big = System.currentTimeMillis();
        //串行执行
        Thread t = new Thread(()->{
            long a = 0;
           for(long i = 0;i<10_0000_0000;i++){
               a++;
           }
        });
        t.start();
        t.join();
        long end = System.currentTimeMillis();
        System.out.println("串行消耗时间:" + (end - big) + "ms");
    }
    public static void func2() throws InterruptedException {
        long big = System.currentTimeMillis();
        Thread t1 = new Thread(()->{
            long b = 0;
        for(long i = 0;i< 10_0000_0000 / 2;i++){
            b++;
        }
        });
        t1.start();
        Thread t2 = new Thread(()->{
            long c = 0;
            for(long i = 0;i<10_0000_0000/ 2;i++){
                c++;
            }
        });
        t2.start();
        t1.join();
        t2.join();
        long end = System.currentTimeMillis();
        System.out.println("并行执行消耗时间:" + (end - big) + "ms");
    }
    public static void main(String[] args) throws InterruptedException {
        func1();//串行执行
        func2();//并行执行
    }
}

Java中Thread类怎么使用和它的属性是什么

我们可以很明显的看出串行时间要比并行时间长的多,串行时间几乎是并行时间的2倍。

Java中Thread类怎么使用和它的属性是什么

Thread类的其他属性和方法

Thread的常见构造方法

属性获取方法
IDgetId()
名称getName()
状态getState()
优先级getPriority()
是否后台线程isDaemon()
线程是否存活isAlive()
线程是否被中断isinterrupted()
  • ID是线程唯一的标识,不同的线程之间不会重复

  • 名称是各种调试工具用到的

  • 状态标识当前线程的一种情况

  • 优先级高的线程,理论上更容易被执行到

  • 是否存活简单理解为run()方法是否执行结束

给一个线程起名字

Thread(String name) 这个东西是给thread对象起一个名字,具体起什么名字和线程的执行效率无关,起名字主要依靠于程序员,方便程序员在后期进行调试。

public class TestDemo17 {
    public static void main(String[] args) {
        //给线程器名字
        Thread t1 = new Thread(()->{
            while(true) {
                System.out.println("hello thread1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"thread1");

        Thread t2 = new Thread(()->{
            while(true) {
                System.out.println("hello thread2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"thread2");
        t1.start();
        t2.start();
    }
}

那么怎样才能看到,我们定义好的线程名字呢?

注意:当我们要查看线程名字的时候,程序必须要正在执行,否则我们查找不到对应的线程名字。

Java中Thread类怎么使用和它的属性是什么

判断一个线程是否存活

简单的说就是操作系统中我们创建出来的线程是否还存在

Thread t 的生命周期和操作系统中对应的线程的生命周期并不是完全一致的。

我们在定义一个线程类后,在调用t.start()方法之前,操作系统中是没有我们创建出来的线程的。在线程类中的run()方法执行完之后,我们在操作系统中创建出来的线程就被销毁了!但是线程t对象还存在。

  • 总而言之,在调用t.start()方法之前,在执行run()方法之后,此时操作系统中是没有我们创建出来的线程的

  • 在调用t.start()方法之后,在指向run()方法之前,此时操作系统中存在我们创建出来的线程 判断该线程是由是后台线程

如果一个线程是后台线程,那么这个线程就不会进行进程退出

如果一个线程是前台线程,那么这个这个线程就会影响到进程的退出。我们以上的代码在创建线程,那些线程都是前台线程,假如现在有前台线程t1,t2, 现在即使main线程执行结束,但是此时还不可以退出线程,必须要将t1,t2线程执行结束之后,整个线程才会结束。

假如现在有两个线程t1,t2,它们都是后台线程,那么如果现在main线程执行结束,整个进程就执行结束,此时我们会强制停止t1,t2线程。

public class TestDemo18 {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
            System.out.println("hello thread");
        });
        t.start();
        System.out.println(t.isDaemon());
    }
}
//因为我们创建的是一个前台线程,所以返回false

Thread的其他常见属性

创建线程

创建线程:定义出一个线程类,然后启动线程t.start(),其中start()方法决定系统是不是真的创建出线程。

Java中Thread类怎么使用和它的属性是什么

线程的中断

中断线程简单的可以理解成为就是让该线程中的run()方法执行结束。还有一个特殊的就是main线程,如果想要中断main线程,那么就需要把main线程执行完。

中断线程方法一:设置一个标志位

public class TestDemo21 {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
           while(!Thread.currentThread().isInterrupted()) {
               System.out.println("hello thread");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });
        //启动t线程
        t.start();
        //在main线程中中断t线程
        //5s之后中断t线程
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.interrupt();
    }
}

运行结果:当t线程中的sout语句被执行5次之后,线程停止。

上面的这种写法不够严谨,只适用于该场合,如果化作是别的代码场合的话,有可能不会终止线程。

这里用一种较好的方法,使用Thread类中自带的检查线程是否断开。

Thread.interrputed() 这是一个静态方法 Thread.currentThread().isinterrupted() 其中Thread.cerrentThread()可以获得线程的引用。

t.interrupted()用于中断线程

public class TestDemo21 {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
           while(!Thread.currentThread().isInterrupted()) {
               System.out.println("hello thread");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });
        //启动t线程
        t.start();
        //在main线程中中断t线程
        //5s之后中断t线程
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.interrupt();
    }
}

Java中Thread类怎么使用和它的属性是什么

Java中Thread类怎么使用和它的属性是什么

在我们上边中断线程,判断标志位的时候,我们使用的是第三种方法。设置标志位的时候使用的是第一种方法。

在我们的日常开发中,经常会用到Thread.currentThread().isInterrupted()来判断标志位,判断该线程是否被中断。

还有一种方法判断标志位,但是它是一个静态方法,只能判断一个类中只有一个线程的情况下,这个方法就是Thread.isinterrupted()方法

Thread.currentThread().isinterrupted()这个方法判断的是Thread的普通成员,每个实例都有一个标志位。

在我们以后就无脑使用Thread.currentThread().isInterrupted()方法,判断线程是否中断(标志位)

线程的等待

在前面我们也介绍到join()方法,这个方法就是让线程与线程之间,有了一定的执行顺序。我们知道在多线程中的调度是随机的,不确定的,多线程的执行靠调度器安排,该调度器的安排是随机的,不规律的。其实线程等待就是一个行之有效的方法,实际上就是控制线程执行的先后顺序。

public class TestDemo22 {<!--{C}%3C!%2D%2D%20%2D%2D%3E--> public static void main(String[] args) throws InterruptedException {<!--{C}%3C!%2D%2D%20%2D%2D%3E--> Thread t = new Thread(()->{<!--{C}%3C!%2D%2D%20%2D%2D%3E--> for(int i = 0;i<5;i++){<!--{C}%3C!%2D%2D%20%2D%2D%3E--> System.out.println("hello thread"); try {<!--{C}%3C!%2D%2D%20%2D%2D%3E--> Thread.sleep(1000); } catch (InterruptedException e) {<!--{C}%3C!%2D%2D%20%2D%2D%3E--> e.printStackTrace(); } } }); t.start(); t.join();//main线程调用t.join()方法,main线程就处于阻塞状态,当t线程执行完后,唤醒main线程执行后序代码 System.out.println("hello main"); }}

Java中Thread类怎么使用和它的属性是什么

获取线程的引用

使用方法Thread.currentTread()就可以该线程的实例。

public class TestDemo23 {
    public static void main(String[] args) {
         Thread t = new Thread(){
             @Override
             public void run() {
                 //获取当前线程的引用
                 //System.out.println(Thread.currentThread().getName());
                 //因为当前使用的匿名内部类是继承自Thread类,Thread就是该匿名内部类的父类,所以可以通过this得到当前Thread的实例
                 System.out.println(this.getName());
             }
         };
         t.start();
    }
}

Java中Thread类怎么使用和它的属性是什么

线程的休眠

其实线程休眠就是调用Thread.sleep()方法。

回顾之前的学习内容,我们知道我们使用PCB描述一个进程,使用双向链表来组织进程。这种说法是针对一个进程中只有一个线程的情况下。

那么如果一个进程中有多个线程存在,那么每个线程就有一个PCB,那么每个进程都会有一组PCB,

PCB上有一个字段为tgroupId,这个id就相当于是进程的id,进程中的每个线程的tgroupId都是相同的。

那么进程控制块(process contral block)和线程有什么关系呢?

其实在linux中是不区分进程和线程的,所谓的线程是程序员自己搞出来的,实际上linux只认进程控制块(PCB),实际上线程就相当于一个轻量级进程。

Java中Thread类怎么使用和它的属性是什么

到此,相信大家对“Java中Thread类怎么使用和它的属性是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI