Java学习之多线程4

线程状态

在这里插入图片描述

线程停止
public class TestStop implements Runnable{

    private boolean flag = true;

    @Override
    public void run() {
        int i = 0;
        while (flag){
            System.out.println("run...."+i++);
        }
    }

    public void stop(){
        this.flag = false;
    }

    public static void main(String[] args) {
        TestStop testStop = new TestStop();
        new Thread(testStop).start();
        for (int i = 0; i < 1000; i++) {
            System.out.println("main----"+i);
            if (i==900){
                testStop.stop();
                System.out.println("线程停止了!");
            }
        }
    }
}

线程休眠

在这里插入图片描述

package PrimeThread.Demo02;
    //模拟网络延时:放大问题的发生性
public class TestSleep implements Runnable{
    //票数
    private int ticketNums = 10;
    @Override
    public void run() {
        while (true){
            if (ticketNums<=0){
                break;
            }
            //模拟延时
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }//线程不安全
            System.out.println(Thread.currentThread().getName()+"-->拿到了第"+ticketNums--+"张票");
        }
    }

    public static void main(String[] args) {
        TestSleep testSleep = new TestSleep();
        new Thread(testSleep,"小明").start();
        new Thread(testSleep,"黄牛").start();
        new Thread(testSleep,"老师").start();
    }
}

public class TestSleep2 {
    


    public static void tenDemo() throws InterruptedException {
        int num = 10;
        while (true){
            Thread.sleep(1000);
            System.out.println(num--);
            if (num==0){
                break;
            }
        }
    }
}
package PrimeThread.Demo02;

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

public class TestSleep2 {
    public static void main(String[] args) {
        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();
            }
        }

    }
}
线程礼让

在这里插入图片描述

package PrimeThread.Demo02;

public class TestYield {
    public static void main(String[] args) {
        MyYield myYield = new MyYield();
        new Thread(myYield,"A").start();
        new Thread(myYield,"B").start();
    }
}
class MyYield implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程开始!");
        Thread.yield();
        System.out.println(Thread.currentThread().getName()+"线程结束!");
    }
}

Join
package PrimeThread.Demo02;
    //测试join方法
public class TestJoin implements Runnable{
        @Override
        public void run() {

            for (int i = 0; i < 1000; i++) {
                System.out.println("vip来了"+i);
            }
        }

        public static void main(String[] args) throws InterruptedException {
            //启动线程
            TestJoin testJoin = new TestJoin();
            Thread thread = new Thread(testJoin);
            thread.start();
            //主线程
            for (int i = 0; i < 1000; i++) {
                if (i==200){
                    thread.join();
                }
                System.out.println("主线程"+i);
            }

        }
    }
线程状态

public class TestState {
    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("|||||||||");
        });

        //观察状态
        Thread.State state = thread.getState();
        System.out.println(state);

        thread.start();
        state = thread.getState();
        System.out.println(state);

        while (state!=Thread.State.TERMINATED){
            Thread.sleep(100);
            state = thread.getState();
            System.out.println(state);
        }
    }
}

优先级

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

        //默认优先级为5
        MyPriority myPriority = new MyPriority();

        Thread t1 = new Thread(myPriority);
        Thread t2 = new Thread(myPriority);
        Thread t3 = new Thread(myPriority);
        Thread t4 = new Thread(myPriority);
        Thread t5 = new Thread(myPriority);
        Thread t6 = new Thread(myPriority);

        t1.start();

        //先设置优先级在启动线程
        t2.setPriority(5);
        t2.start();

        t3.setPriority(10);
        t3.start();

        t4.setPriority(1);
        t4.start();

        t5.setPriority(8);
        t5.start();

        t6.setPriority(9);
        t6.start();
    }
}
class MyPriority implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"---->"+Thread.currentThread().getPriority());
    }
}
守护线程
public class TestDaemon {
    public static void main(String[] args) {
        God god = new God();
        You1 you1 = new You1();

        //创建守护线程
        Thread thread = new Thread(god);
        thread.setDaemon(true);

        //启动上帝守护线程
        thread.start();

        //线程启动
        new Thread(you1).start();
    }
}
//上帝
class God implements Runnable{
    @Override
    public void run() {
        System.out.println("上帝守护着你!");
    }
}
//人
class You1 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 26500; i++) {
            System.out.println("每天都要努力呀!");
        }
        System.out.println("Good Bay! World!");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

? Adore ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值