1. 线程停止

package cn.hm1006.demo;

public class ThreadTest implements Runnable{
    private boolean flag = true;
    @Override
    public void run() {
        int i = 0;
        while (flag){
                System.out.println("线程在运行");
        }
    }
    /*停止标识*/
    public void threadStop(){
        this.flag = false;
    }

    public static void main(String[] args) {
        ThreadTest threadTest = new ThreadTest();
        new Thread(threadTest).start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("main方法运行:"+i);
            if (i == 900){
                threadTest.threadStop();
                System.out.println("线程停止!");
            }
        }
    }
}

image.png

2. 线程休眠(sleep)

package cn.hm1006.demo;

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

public class ThreadTest02{


    public static void main(String[] args) {
        tenDown();
        //打印系统时间
        Date date = new Date(System.currentTimeMillis());
        while (true){
            try {
                Thread.sleep(1000);
                System.out.println(new SimpleDateFormat("HH:mm:ss").format(date));
                date = new Date(System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
    //模拟倒计时
    public static void tenDown() {

        for (int i = 0; i <= 10; i++) {
            try {
                Thread.sleep(1000);
                System.out.println(i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

//模拟倒计时
image.png
//打印系统时间
image.png

3. 线程强制执行(json)

package cn.hm1006.demo;

public class ThreadTest03 implements Runnable{

    //vip线程
    @Override
    public void run() {
        for (int i = 0; i < 300; i++) {
            System.out.println("vip线程"+i);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadTest03 threadTest03 = new ThreadTest03();
        Thread thread = new Thread(threadTest03);

        for (int i = 0; i < 500; i++) {
            System.out.println("mian主线程"+i);
            if (i==100){
                thread.start();
                thread.join();
            }
        }
    }
}

4. 线程状态(state)

package cn.hm1006.demo;

public class Thread4State{


    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(11111111);
        });
        //线程状态:
        Thread.State state = thread.getState();
        System.out.println(state);//状态:NEW

        //观察线程启动后的状态:
        thread.start();
        state = thread.getState();
        System.out.println(state);//状态:RUNNABLE

        //线程停止
        while (state!=Thread.State.TERMINATED){
            Thread.sleep(100);
            state = thread.getState();
            System.out.println(state);//状态:TERMINATED
        }

    }
}

5. 设置线程优先级(priority)

优先级别高,也不一定先执行

package cn.hm1006.demo;

public class Thread5priority {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName()+"->"+Thread.currentThread().getPriority());

        Mypriority mypriority = new Mypriority();
        Thread thread1 = new Thread(mypriority);
        Thread thread2 = new Thread(mypriority);
        Thread thread3 = new Thread(mypriority);
        Thread thread4 = new Thread(mypriority);
        Thread thread5 = new Thread(mypriority);

        thread1.start();
        //先设置优先级,再启动
        thread2.setPriority(3);
        thread2.start();

        thread3.setPriority(Thread.MAX_PRIORITY);
        thread3.start();

        thread2.setPriority(1);
        thread4.start();

        thread2.setPriority(9);
        thread5.start();


    }


    private static class Mypriority implements Runnable{
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());
        }
    }
}

6. 开启守护线程(Daemon)

thread.setDaemon(true);//开启守护线程,默认所有用户为flase。

package cn.hm1006.demo;

public class Thread6Daemon {

    public static void main(String[] args) {
        God god = new God();
        Thread thread = new Thread(god);
        thread.setDaemon(true);//开启守护线程,默认所有用户为flase。
        thread.start();//

        You you = new You();
        new Thread(you).start();


    }
}

class God implements Runnable {
    @Override
    public void run() {
        while (true) {
            System.out.println("上帝永生");
        }
    }
}

class You implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 36500; i++) {
            System.out.println("活着");
        }
        System.out.println("-------bye bye------");
    }
}

7.线程同步(sysn)


Q.E.D.


如人饮水、冷暖自知