Thread 类的基本用法

创建线程的五种方法:

继承 Thread, 重写 run

class MyThread  extends Thread{//继承

    @Override//重写
    public void run() {
        while(true) {
            System.out.println("hello");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {//中断异常
                e.printStackTrace();
            }
        }
    }
}
public class ThreadDemo {
    public static void main(String[] args) {//主线程
        Thread t = new MyThread();
        t.start();//新线程

        while(true){
            System.out.println("main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

实现 Runnable, 重写 run

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

        while(true) {
            System.out.println("main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

继承 Thread, 重写 run, 使用匿名内部类

public class ThredDemo3 {
        public static void main(String[] args) {
                Thread t = new Thread() {
                        //匿名内部类

                        @Override
                        public void run() {
                                while(true) {
                                        System.out.println("hello t");
                                        try {
                                                Thread.sleep(1000);
                                        } catch (InterruptedException e) {
                                                e.printStackTrace();

                                        }
                                }
                        }
                };

                t.start();

                while(true) {
                        System.out.println("main");
                        try {
                                Thread.sleep(1000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                }
        }
}

实现 Runnable, 重写 run, 使用匿名内部类

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

        while(true) {
            System.out.println("main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

使用 lambda 表达式

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

        t.start();
        while(true){
            System.out.println("main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

线程的中断

让线程的入口方法执行完毕

给线程中设定一个结束标志位

public class TestDemo2 {
public static boolean isQuit = false;

    public static void main(String[] args) {
        Thread thread = new Thread(()->{
            while(!isQuit){
                System.out.println("hello t");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("END");
        });

        thread.start();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        isQuit = true;
    }
}

运用interrupt

Thread.currentThread().isInterrupted()
( isInterrupted 标志位 = false)
( intrerupt = ture)在这里插入图片描述
(!false)-> (! true)

public class TestDemo2 {
//public static boolean isQuit = false;

    public static void main(String[] args) {
        Thread thread = new Thread(()->{
            while(!Thread.currentThread().isInterrupted()){
                //isInterrupted 标志位 = false
                System.out.println("hello t");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    //抛出异常
                    break;
                }
            }

            System.out.println("END");

        });

        thread.start();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            //打印出异常位置的调用栈
        }
//        isQuit = true;
        thread.interrupt();
        //清空标志位为 ture
    }
}

在这里插入图片描述

线程的等待

有时,我们需要等待一个线程完成它的工作后,才能进行自己的下一步工作。
(join 方法)使该线程 阻塞 等待

public class TestDEMO3 {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
            System.out.println("hello t");
        });
        thread.start();

        thread.join();

        System.out.println("main");
    }
}

让 start 创建的新线程等待 hello t 打印完后 再执行
在这里插入图片描述

获取线程实例

你在哪个线程中去调用这个方法,那么它就会返回这个线程的实例对象.

public class ThreadDemo11 {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            System.out.println(Thread.currentThread().getName());
        },"1线程");
        t.start();
    }
}

在这里插入图片描述

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值