线程的常用方法一

线程

停止线程比较好的方法

1.不要使用JDK已经废弃的stop()等方法。

2.设置外部标志位,控制线程运行,理论上是让他自己停止

3.让他自己停下来

public class ThreadStop implements Runnable{
    private boolean flag = true;
    @Override
    public void run() {
        int i = 0;
        while (flag){
            System.out.println("新线程在运行·········"+i);
        }
    }
    public void stop(){//通过外部标志位来结束线程
        this.flag = false;
    }
    public static void main(String[] args) {
        ThreadStop threadStop = new ThreadStop();
        new Thread(threadStop).start();
        for (int i = 0; i < 100; i++) {
            System.out.println("main"+i);
            if(i==90){
                threadStop.stop();
                System.out.println("线程该停止了");
            }
        }
    }
}

模拟延迟

Thread.sleep(1000);//它不仅仅可以让线程延迟,他可以让他所在的代码块延时1000ms
package Thread;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ThreadSleep {
    public static void main(String[] args) {
        Date starttime = new Date(System.currentTimeMillis());
        while (true){
            try{
                Thread.sleep(1000);
                System.out.println(new SimpleDateFormat("HH:mm:ss").format(starttime));
                starttime = new Date(System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

yield()方法,礼让。

线程礼让yield。未必会成功,礼让是两个先出来,然后再进去,谁先进去也不确定

Thread.yield();//就像跑步比赛,小明在前面跑,小王追不上。现在裁判(yield())把他们喊到起点,小王和小明跑的更快全靠运气。

join()方法,插队法

public class ThreadJoin implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("我插队进来的"+i);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadJoin threadJoin = new ThreadJoin();
        Thread thread = new Thread(threadJoin);
        thread.start();
        for (int i = 0; i < 20; i++) {
            if(i==10){
                thread.join();
            }
            System.out.println("MainThread在运行" + i);
        }
    }
}

备注:插队只能让他马上执行,执行多久,是看运气的。可能他本来就可以执行,甚至本来就在执行

线程状态

  • lambda表达式,获取一个线程的对象可方便了
  • Thread.State是枚举类型,分别是线程的几种状态
  • thread.getState()获取的是一个进程当前的状态 thread是一个进程对象名。
  • 线程死亡之后不可能再被创建
Thread.State state = thread.getState();//state座位进程状态枚举对象,把thread对象.getState赋值给state。
public class ThreadState {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{//lambda表达式,获取一个线程的对象可方便了
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("for循环结束了,线程不要阻塞了");
        });     
        System.out.println(state);//初始线程的状态
        thread.start();//启动线程
        System.out.println(thread.getState());//获取执行后的状态
        while (thread.getState() != Thread.State.TERMINATED){
            Thread.sleep(1000);
            System.out.println(thread.getState());//获取当前的状态
        }
    }
}

线程优先级

先设置优先级,然后再启动。

优先级数大,优先级高,优先被执行。但是低的未必一定会后执行。只是权重低而已。

t1.setPriority(优先级数);越大越优先。
package Thread;
public class ThreadPriority{
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName()+"----->"+Thread.currentThread().getPriority());
        Priority priority = new Priority();
        Thread t1 = new Thread(priority);
        Thread t2 = new Thread(priority);
        Thread t3 = new Thread(priority);
        Thread t4 = new Thread(priority);
        Thread t5 = new Thread(priority);
        Thread t6 = new Thread(priority);
        Thread t7 = new Thread(priority);
        t1.setPriority(1);
        t1.start();
        t2.setPriority(2);
        t2.start();
        t3.setPriority(3);
        t3.start();
        t4.setPriority(4);
        t4.start();
        t5.setPriority(5);
        t5.start();
        t6.setPriority(6);
        t6.start();
        t7.setPriority(7);
        t7.start();
    }
}
class Priority implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"----->"+Thread.currentThread().getPriority());
    }
}
posted @ 2020-04-16 23:23  li33的博客  阅读(98)  评论(0编辑  收藏  举报