有关线程的一些方法

135 阅读1分钟

方法及简单代码详解

  1. 线程停止
package com.Thread;

//线程停止
//1.建议线程正常停止 -->利用次数,不建议死循环。
//2.建议使用标志位-->设置一个标志位
//3.不要使用stop或者destroy等过时或者JDK不建议使用的方法。


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 < 1000; i++) {
            System.out.println("main线程"+i);
            if (i==800){
                threadStop.stop();//调用stop方法,停止线程
                System.out.println("线程停止");
            }
        }
    }


}
  1. 线程休眠(每一个对象都有一个锁,sleep不会释放锁)
package com.Thread;

//多线程同时控制一个对象
//买火车票的例子

//发现问题: 多线程控制一个对象,不安全,出现数据紊乱的情况
public class threadTest3 implements Runnable{

    private int tickerNumber = 10;

    @Override
    public void run() {
        while (tickerNumber>=1){

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

            System.out.println(Thread.currentThread().getName()+"-->得到第"+tickerNumber+"张票");
            tickerNumber--;
        }
    }

    public static void main(String[] args) {
        threadTest3 threadTest3 = new threadTest3();

        new Thread(threadTest3,"小盆友").start();
        new Thread(threadTest3,"黄牛").start();
        new Thread(threadTest3,"农民工").start();
    }



}
  1. 线程礼让
package com.Thread;

//线程礼让:礼让不一定成功,看CPU心情
public class ThreadYield 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) {
        ThreadYield threadYield = new ThreadYield();
        new Thread(threadYield,"a").start();
        new Thread(threadYield,"b").start();
    }


}
  1. 线程强制执行
package com.Thread;

public class ThreadJoin implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 200; i++) {
            System.out.println("vip来了"+i);
        }
    }


    public static void main(String[] args) {
        //启动线程
        ThreadJoin threadJoin = new ThreadJoin();
        Thread thread= new Thread(threadJoin);
        thread.start();

        //主线程
        for (int i = 0; i < 1000; i++) {
            if (i==100){
                try {
                    thread.join();//线程强制执行:可以想象成插队
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("主线程正在执行"+i);
        }

    }


}
  1. 线程状态
package com.Thread;

import org.omg.PortableServer.THREAD_POLICY_ID;

public class ThreadTest{

    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();//new
        System.out.println(state);

        //观察启动后
        thread.start();//启动线程
        state = thread.getState();//更新线程状态
        System.out.println(state);//run

        while(state != Thread.State.TERMINATED){
            try {
                Thread.sleep(100);
                state = thread.getState();//更新线程状态
                System.out.println(state);//输出状态
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }
}