Thread 类

简介: Thread 类

hello,大家好,好久不见,甚是想念,今天为大家带来Thread类的相关知识点

🐷线程创建

🐷线程中断

🐷线程等待

🐷线程休眠

🐷获取线程实例


线程创建

线程创建有五种方式

1.继承Thread方法,重写run方法

2.实现runnable方法,重写run

3.继承Thread,使用匿名内部类

4.实现Runnable,使用匿名内部类

5.使用lambda表达式

这5个里面用的最多的还是第五个,下面我们一个一个来看

🎉继承Thread方法,重写run方法

package threading;
/**
 * Created with IntelliJ IDEA.
 * Description:演示多线程的基本创建方式
 * User: WHY
 * Date: 2022-09-02
 * Time: 9:46
 */
class MyThread extends Thread {//因为Thread这个类是标准库中的类,而自己写的MyThread 类继承了父类,所以要重写这个
    @Override
    public void run() {//重写run方法。run方法是thread父类里面已有的方法,此时重写他
        while (true) {
            System.out.println("hello  thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);//因为run方法重写了,所以在抛异常的时候不能选择用throws,只能try  catch;
            }
        }
    }
}
public class Demo1 {
    public static void main(String[] args) {
    MyThread t=new MyThread();//实例化的时候其实并没有启动线程
    t.start();//启动了这个线程
        while(true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}


2.实现runnable方法,重写run


package threading;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2022-09-03
 * Time: 21:38
 */
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 {
    //创建线程不仅可以用继承Thread,还有一种方法是实现Runnable接口,重写run
    public static void main(String[] args) {
        MyRunnable   runnable=new MyRunnable();
        Thread  t=new Thread(runnable);
        t.start();
        while(true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}


3.继承Thread,使用匿名内部类


public class demo1 {
    public static void main(String[] args) {
        System.out.println("hello main");
        new Thread(){
            @Override
            public void run() {
                System.out.println("hello thread");
                for(int i = 0;i<4;i++){
                    System.out.println("hello ");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("子线程运行结束");
            }
        }.start();
        for(int i = 0;i<4;i++){
            System.out.println("Main线程");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }   
        }
        System.out.println("线程已经结束了");
    }
}


4.实现Runnable,使用匿名内部类


public class demo {  
    public static void main(String[] args) {     
        // 创建一个线程,参数是实现了Runnable接口的匿名类的实例  
        Thread thread = new Thread(new Runnable() {   
            public void run() {   //实现run() 方法 
                System.out.println("hello thread");  
            }  
        });  
        thread.start();                         // 启动线程  
    }  
} 


5.使用lambda表达式


public class demo10 {
    public static void main(String[] args) {
        Thread t=new Thread(()->{
            while(!isQuit){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程执行完了");
        },"这是我的线程");
        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("设置让t线程结束");
    }
}


其中lambda表达式是最常用的那个

线程中断


package threading;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2022-09-11
 * Time: 14:43
 */
public class demo10 {
    public static boolean isQuit=false;
    public static void main(String[] args) {
        Thread t=new Thread(()->{
            while(!isQuit){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程执行完了");
        },"这是我的线程");
        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        isQuit=true;
        System.out.println("设置让t线程结束");
    }
}


package threading;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2022-09-11
 * Time: 14:57
 */
public class demo11 {
    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();
                    break;
                }
            }
            System.out.println("线程执行完了");
        },"这是我的线程");
        t.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t.interrupt();//主线程通过这个中断线程,设置标志位为true
        System.out.println("设置让t 结束");
    }
}


线程等待


public class demo12 {
    public static void main(String[] args) {
        Thread t=new Thread(()->{
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"这是我的代码");
        t.start();
        System.out.println("join之前");
        try {
            t.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("join之后");
    }
}


线程休眠

这个很简单没啥好说的,就是单纯地Thread.sleep();但是要抛出一个异常

线程创建一个实例


class Counter{
    public int  count;
    public  synchronized   void increase(){
        count++;
    }
}
public class demo14 {
    private static  Counter counter=new Counter();
    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            for(int i=0;i<50000;i++){
                counter.increase();
            }
        });
        Thread t2=new Thread(()->{
            for(int i=0;i<50000;i++){
                counter.increase();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("counter:"+counter.count);
    }
}


使用lambda表达式来创建一个t1,t2实例


public class demo10 {
    public static boolean isQuit=false;
    public static void main(String[] args) {
        Thread t=new Thread(()->{
            while(!isQuit){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程执行完了");
        },"这是我的线程");
        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        isQuit=true;
        System.out.println("设置让t线程结束");
    }
}


如上图代码,创建了t实例

今天的讲解就到此为止,我们下期再见,886!!!

相关文章
|
1月前
|
Java 程序员 调度
Thread类及常见方法
Thread类及常见方法
|
8月前
|
Java 程序员 调度
了解Thread类的其他一些方法及常见属性
了解Thread类的其他一些方法及常见属性
40 0
|
8月前
|
Java 调度
Thread常用方法
Thread常用方法
43 0
|
9月前
|
Java 调度
Thread类的方法
Thread类的方法
29 0
|
9月前
Thread 类的基本用法
比较推荐:使用 lambda 表达式创建线程的时候不用重写 run 方法。 不需要显式重写run方法的原因是因为线程的目标方法已经在Lambda表达式中定义了。Lambda表达式是一种用于创建匿名函数的语法糖,它可以将一个方法(或一段代码块)包装为一个函数对象。当您使用Lambda表达式创建线程时,Lambda表达式的内容会被视为线程执行的任务,这个任务会自动成为run方法的实现。
43 0
|
11月前
Thread类的基本用法
Thread类的基本用法
|
程序员 调度
Thread类的其它方法和属性
Thread类的其它方法和属性
Thread类的其它方法和属性
Java多线程的创建与Thread类的方法及使用(下)
Java多线程的创建与Thread类的方法及使用(下)
Java多线程的创建与Thread类的方法及使用(下)