Thread类的基本用法

1. 线程创建

一般线程的创建有五种

1.1 继承Thread, 重写 run 方法

class MyThread extends Thread{
    public void run() {
        while (true) {
            System.out.println("holle,z");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
public class ThreadDemo1 {
    public static void main(String[] args) {
        Thread t = new MyThread();
        t.start();
        while (true){
            System.out.println("holle,l");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 1.2 实现 Runnable 接口,重写 run 

class MyRunnable implements Runnable {
    @Override
    public void run() {
        while (true){
            System.out.println("holle,z");
            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("holle,l");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

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

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

1.4 实现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("holle,z");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
        while (true){
            System.out.println("holle,l");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

1.5 lambda 表达式

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

2. 线程中断

顾名思义就是线程执行完毕

1. 自己手动设置一个标志位(变量boolean)终止线程

多个线程使用同一份内存地址,故 main 和 t 线程判定同一个 标志位(isquit)

2. 调用 Thread 自己的标志位

Thread.interrupted()

调用这个方法会有两种情况

1. 线程是就绪状态,把标志位 置为 true 

2. 如果线程的阻塞状态就会出发异常

public static void main(String[] args){
        Thread t = new Thread(() -> {
            System.out.println("bit");
            //currentThread()获取当前对象实例
            //此处currentThread获取的对象是 t
            //isInterrupted 是t 对象自带的标志位
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        });
        //System.out.println(t.getName());
        t.start();
        //t.join();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.interrupt();
        System.out.println("holle");
    }

3. 线程等待

顾名思义,让其中一个 线程先执行,其余的线程等待

public static void main(String[] args) throws InterruptedException{
        Thread t = new Thread(() -> {
            System.out.println("bit");
            //currentThread()获取当前对象实例
            //此处currentThread获取的对象是 t
            //isInterrupted 是t 对象自带的标志位
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        });
        t.start();
        t.join();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //t.interrupt();
        System.out.println("holle");
    }

这里就是 t 线程先执行,然后执行main 里面的线程

4. 线程休眠

public static void main(String[] args) throws InterruptedException{
        Thread t = new Thread(() -> {
            System.out.println("bit");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }            
        });
        t.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
      
        System.out.println("holle");
    }

这里 t 线程 休眠了一秒打印 bit ,随后 main 里面的线程休眠了两秒打印 holle  

5. 获取线程实例

 public static void main(String[] args) {
        Thread t = new Thread(() -> {
            System.out.println(Thread.currentThread());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t.start();
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值