多线程几种常用方法代码

线程只能start一次
1.sleep,休眠

//增加事故发生问题
public class TestSleep implements Runnable{
    //票
    private int ticketNum=10;
    @Override
    public void run() {
        while(true){
            if(ticketNum<=0){
                break;
            }

            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"---拿到了票:"+ticketNum--);
        }
    }

    public static void main(String[] args) {
        TestThread4 testThread4=new TestThread4();
//可用共同资源
        new Thread(testThread4,"张三").start();
        new Thread(testThread4,"李四").start();
        new Thread(testThread4,"王五").start();
    }
}

2.yield(),礼让,当前线程出来,在公平竞争

//出来就是公平竞争,礼让不会一定成功
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()+"线程结束");
    }
}

3.join()插队vip

//插队vip,可能会阻严重
public class TestJoin implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("vip插队"+i);
        }
    }

    public static void main(String[] args) {
        TestJoin testJoin=new TestJoin();
        Thread thread = new Thread(testJoin);
        thread.start();

        for (int i = 0; i < 20; i++) {
            if(i==10){
                try {
                    thread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("main线程"+i);
        }

    }

}

4.stop终止线程

//1.次数
// 2.标志位
//3.不建议使用stop,story,等
public class TestStop implements Runnable{
    //1.设置标志位
    private boolean flag=true;
    @Override
    public void run() {
        int i=0;
        while (flag){
            System.out.println("run...Thread"+i++);
        }
    }
//2.停止线程的方法,转换标志位
    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 < 2000; i++) {
            System.out.println("main"+i);
            if(i==1990){
                //调用标志位转换,停止
                testStop.stop();
            }
        }
    }
}

5.getState(),setState()获取线程状态

//观测状态
public class TestState {
    public static void main(String[] args) {
        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);//new

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

        //线程不终止
        while(state!=Thread.State.TERMINATED){//终止的静态变量
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            state=thread.getState();//更新!!!
            System.out.println(state);
        }
        //线程只执行一次,不会两次start()
//        thread.start();
    }
}

6.setPriority,设置优先级

public class TestYield {
    public static void main(String[] args) {
        MyYield myYield=new MyYield();

        Thread thread = new Thread(myYield, "a");

        Thread thread1 = new Thread(myYield, "b");
        thread.setPriority(10);
        thread1.setPriority(1);
        thread.start();
        thread1.start();
    }

}
class MyYield implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程");

    }
}

运行结果经常是:

a线程
b线程

因为优先级1-10,默认5,数字越大,越容易运行,当然也不绝对。

你可能感兴趣的:(多线程几种常用方法代码)