多线程状态

stop()不推荐使用

package test2;

public class TestStop implements Runnable{

    private boolean flag=true;

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

    public static void main(String[] args) {
        TestStop testStop=new TestStop();
        Thread thread=new Thread(testStop);
        thread.start();
        for (int i = 0; i < 1000; i++) {
            System.out.println("main线程运行"+i);
            if (i==500){
                testStop.stop();
                System.out.println("线程该停止了");
            }
        }
    }
    public void stop(){
        this.flag=false;
    }
}

sleep()使线程为阻塞状态【单位:毫秒--->1000毫秒=1秒】

  • 时间到达后回恢复到就绪状态

  • 每一个对象都有一个锁,sleep()不会释放锁

  • 可以模拟网络延时,倒计时

网络延时例子:

package test2;

//模拟网络延时(控制下载速度等功能)
//该例子存在并发的问题
public class TestSleep implements Runnable{

    private int ticketNum=1;
    @Override
    public void run() {

        while(true){
            if(ticketNum>10){break;}

            //模拟延时
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

                System.out.println(Thread.currentThread().getName()+"抢到了第"+ticketNum+++"张车票");

        }
    }

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

    }

}

倒计时例子:

package test2;

//模拟倒计时
public class TestSleep2 implements Runnable{

    @Override
    public void run() {

        try {
            tenDown(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        TestSleep2 testSleep2=new TestSleep2();
        Thread thread=new Thread(testSleep2);
        thread.start();
    }

    public static void tenDown(int num) throws InterruptedException {

        while(num>0){
            Thread.sleep(1000);
            System.out.println("倒计时第"+num--+"秒");
        }
    }
}

打印当前系统时间的例子:

package test2;

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

//打印当前系统时间
public class TestSleep2 {


    public static void main(String[] args) throws InterruptedException {

        //获取当前系统时间
        Date startTime= new Date(System.currentTimeMillis());
        while(true){
            Thread.sleep(1000);
            System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
            startTime= new Date(System.currentTimeMillis());//更新当前时间

        }
    }


}

yield()线程礼让

  • 礼让不一定成功,只是让线程从运行回到就绪状态【线程暂停,不阻塞】,和其他就绪线程一样等待运行
package test2;

public class TestYield implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程开始执行");
        Thread.yield();
        System.out.println(Thread.currentThread().getName()+"线程停止执行");
    }

    public static void main(String[] args) {
        TestYield testYield=new TestYield();
        new Thread(testYield,"A").start();
        new Thread(testYield,"B").start();

    }
}

join()线程插队

package test2;

public class TestJoin implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 500; i++) {
            System.out.println("线程vip来插队啦!"+i);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TestJoin testJoin=new TestJoin();
        Thread thread=new Thread(testJoin);//thread代理testJoin执行
         thread.start();

         //主线程
        for (int i = 0; i < 400; i++) {

            if (i==200)
            {
                thread.join();
            }
            System.out.println("主线程"+i);
        }
    }
}

posted @ 2021-07-09 14:49  卡卡发  阅读(38)  评论(0)    收藏  举报