Thread类的基本用法(Java)

目录

  1. 线程创建
  2. 线程中断
  3. 线程等待
  4. 线程休眠
  5. 获取线程实例

1.线程创建

线程创建主流有五种常见的方法

第一种:定义了一个继承自 Thread 类的 Mythread 类,该类重写父类了 run 方法,在main线程中创建Mythread线程的实例;

class Mythread extends Thread{
    @Override
    public void run() {
        while(true){
            System.out.println("Hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class demo1 {
    public static void main(String[] args) {
        Thread thread=new Mythread();
    }
}

第二种:定义了一个实现了 Runnable 接口的 MyRunnable 类,在 main 方法中,你创建了一个 Thread 的实例,并将 MyRunnable 对象作为参数传递给 Thread 的构造函数;

class MyRunnable implements Runnable{
    @Override
    public void run() {
        while(true){
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class demo2 {
    public static void main(String[] args) {
        Thread t=new Thread(new MyRunnable());
    }
}

第三种与第一种类似,不过使用的是匿名内部类的写法

public class demo3 {
    public static void main(String[] args) {
        Thread thread =new Thread(){
            @Override
            public void run() {
                while(true){
                    System.out.println("hello thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        thread.start();
    }
}

第四种与第二种相似,使用的同样是匿名内部类的写法

public class demo4 {
    public static void main(String[] args) {
        Thread t=new Thread(new Runnable(){
            @Override
            public void run() {
                while(true){
                    System.out.println("hello thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        t.start();
    }
}

第五种就是使用lambda表达式

public class demo5 {
    public static void main(String[] args) {
        Thread t=new Thread(()->{
            while(true){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t.start();
    }
}

以上五种方式都可以创建一个线程,以上五种基本够用了;

2.线程中断

java中终断线程只是外部给线程一个提示,最终要不要终止还要靠线程本身自己决定;

看一下代码:一秒之后t线程就会停止

public class demo10 {
    private static boolean isRunning=true;
    public static void main(String[] args) throws InterruptedException {
        Thread t=new Thread(){
            @Override
            public void run() {
                while(isRunning){
                    System.out.println("hello thread");
                }
            }
        };
        t.start();
        Thread.sleep(1000);
        isRunning=false;
    }
}

在看代码:t线程打印一个之后,就等待10s,主线程在3秒之后就会提示线程要停止,此时t线程要大概再等7s才会停止;

public class demo10 {
    private static boolean isRunning=true;
    public static void main(String[] args) throws InterruptedException {
        Thread t=new Thread(){
            @Override
            public void run() {
                while(isRunning){
                    System.out.println("hello thread");
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        t.start();
        Thread.sleep(3000);
        isRunning=false;
    }
}

其实Thread提供的interrupt方法和isInterrtputted方法能够更好的实现以上的效果

刚才我们定义了一个boolean变量,实际上Thread内置了一个相似的变量,使用内置的标志位,功能要更加强大;

public class demo10{
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(){
            @Override
            public void run() {
               while(!Thread.currentThread().isInterrupted()){
                   System.out.println("hello thread");
                   try {
                       Thread.sleep(1000);
                   } catch (InterruptedException e) {
                       throw new RuntimeException(e);//决定thread的终止是否
                   }
               }
            }
        };
        thread.start();
        Thread.sleep(3000);
        thread.interrupt();
    }
}

Thread.currentThread()能够获取调用的线程的对象,如代码是在thread中调用的,Thread.currentThread()指向的就是thread线程;如果是放在main线程中,指向的就是main线程;isInterrupted()为true,就是要终止线程,为false就是要线程继续;改代码是在3秒之后提示thread线程要终止了,如果thread线程中没有sleep()线程就会立即停止,但是如果此时线程正处在sleep中就会立即唤醒sleep(),捕捉到InterruptedException异常,按照上面的代码,就会理解抛出一个异常然后终止线程,如果将throw new RuntimeException(e)改成e.printStackTrace()就只是打印出报错信息,然后继续执行while,继续打印hellot hread,所以当使用interrtupt()方法之后,thread线程要不要结束都是自己决定;

此时你会有疑惑,代码不是改了标志为了,下一轮循环应该会立即结束才对呀?但是为什么会继续打印?实际是因为sleep在搞鬼,如果线程中没有sleep方法,修改完标志位t线程就会结束了,但是有sleep方法,且修改标志位是线程正处在sleep,sleep被唤醒的同时,还会清除刚才修改的标志位,又改回false;

后期我们自己在做开发的时候,就可以自己写代码决定线程被提示终止之后要怎么做?如上面的代码就表示程序想这个线程提示终止之后就抛出一个异常之后结束;如果只是打印异常信息,就会表示程序员想这个线程被提示终止之后只是打印出异常信息,然后继续执行;如果直接修改成break,就表示程序员想被提示终止之后就直接直接结束while()方法; 

3.线程等待

其实就是等待线程

直接看代码:没有等待线程的话就是t线程和main线程并发执行,同时打印

public class demo11 {
    public static void main(String[] args) {
        Thread t=new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    System.out.println("hello thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        t.start();
        /*try {
            t.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }*/
       for (int i = 0; i < 5; i++) {
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

2.如果有等待线程,在main线程中调用t.join(),意思就是main线程要等待t线程执行完才能继续执行,所以执行结果就是:

注意:一定要线程调用start方法才能调用join方法,以便确保 main 线程能够正确等待 t 线程的执行。否则可能会抛出异常;

4.线程休眠

在Java中,线程休眠是通过调用Thread.sleep()方法来实现的。这个方法会使当前线程进入休眠状态,暂时停止执行一段时间。它的调用方式如下:

try {
    Thread.sleep(milliseconds); // 休眠指定的毫秒数
} catch (InterruptedException e) {
    // 处理中断异常
}

其中,milliseconds 参数指定了线程休眠的时间,单位是毫秒。在指定的时间到达或被其他线程中断时,线程会从休眠状态恢复,继续执行后续的代码。需要注意的是,Thread.sleep()方法可能会抛出InterruptedException异常,因此需要进行异常处理。
下面是一个简单的示例,演示了如何在Java中使用Thread.sleep()方法使线程休眠:

public class SleepExample {
    public static void main(String[] args) {
        System.out.println("Start of the program");

        try {
            // 休眠3秒钟
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // 处理中断异常
            e.printStackTrace();
        }

        System.out.println("End of the program");
    }
}

上面的示例中,程序在执行到Thread.sleep(3000)时会休眠3秒钟,然后继续执行后续代码。

5.获取线程实例

前面有说到在哪个线程中调用Thread.currentThread(),返回的就是这个线程的实例

Thread.currentThread() 是一个静态方法,它返回当前正在执行的线程对象。在多线程环境下,每个线程都有自己的执行路径,因此可以通过这个方法来获取当前线程的引用。
 

Thread currentThread = Thread.currentThread();
System.out.println("Current thread: " + currentThread.getName());

上面的代码将打印出当前线程的名称。通常情况下,主线程的名称为 "main",其他线程的名称由创建时指定或由 JVM 自动生成。在多线程编程中,Thread.currentThread() 方法通常用于获取当前线程的信息或进行一些与线程相关的操作,例如检查线程状态、设置线程优先级等。

  • 23
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a添砖Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值