65.【 多线程2】(三)

简介: 65.【 多线程2】

(四)、五大线程状态

14.状态顺序

15.线程方法

1.setPriority( int newOriority)   更改线程优先级
2.static void sleep (long mills)  在指定的毫秒内礼让数据
3.void join()   等待该进程终止
4.static void yield()   暂停当前正在执行的线程对象,并执行其他线程
5.void interrupt()  中断线程,别用这个方式
6.boole isAlive()    测试线程是否处于活动状态 
15.1停止线程(false)
1.不推荐使用JDK提供的stop()、destory()方法
2.推荐线程自己停止下来
3.建议使用一个标志位进行终止变量,即flag=false;
public class Demo9 implements Runnable{
    //1.设置一个标识位
    private boolean flag=true;
    @Override
    public void run() {
         int i=0;
        while (flag){
            System.out.println("run......thread "+i++);
        }
    }
    public void stop(){
        this.flag=false;
    }
    public static void main(String[] args) {
        Demo9 demo9=new Demo9();
        //1.开启线程
        new Thread(demo9).start();
        for (int i = 0; i <1000 ; i++) {
            if (i==900){
                //调用stop方法进行线程停止
                demo9.stop();
                System.out.println("线程已经停止");
            }
            System.out.println("main "+i);
        }
    }
}

15.2线程休眠(sleep)
1.sleep() 指定当前线程阻塞的毫秒数
2.sleep()存在异常interruptedException;
3.sleep时间达到后线程就进入就绪状态
4.sleep可以模拟网络延时,倒计时等  (放大问题的发生性)
5.每个对象都有一个锁,sleep不会释放锁
利用线程模拟倒计时的操作:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;
public class Demo10 {
    public static void main(String[] args) throws Exception {
       // tendown();
        //打印系统当前时间
        Date start=new Date(System.currentTimeMillis());
        while (true){
            try {
                Thread.sleep(1000);
                System.out.println(new SimpleDateFormat("HH:mm:ss").format(start));  //打印时间
                 start=new Date(System.currentTimeMillis()); //更新时间
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static void tendown() throws Exception{
        int num=10;
        while (true){
            Thread.sleep(1000);
            System.out.println("倒计时:"+num--);
            if(num<=0){
                break;
            }
        }
    }
}

15.3线程礼让(yield)
1.礼让线程,让当前正在执行的线程暂停,但不阻塞
2.将线程从运行状态转为就绪状态
3.让cpu重新调度,礼让不一定成功。
public class Demo11 {
    public static void main(String[] args) {
        my_Yield my_yield=new my_Yield();
        new Thread(my_yield,"A").start();
        new Thread(my_yield,"B").start();
    }
}
class my_Yield implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"执行开始");
        Thread.yield(); //礼让
        System.out.println(Thread.currentThread().getName()+"执行结束");
    }
}

15.4线程强行执行(join)
1.join 合并线程,待此线程执行完成后,在执行其他的线程,其他线程阻塞
2.可以想象成vip
public class Demo12 implements Runnable{
    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int  i=0;i<100;i++) {
            System.out.println("线程VIP来了");
        }
    }
    public static void main(String[] args) throws Exception {
        Demo12 demo12=new Demo12();
        Thread thread=new Thread(demo12);
        thread.start();
//主线程
        for (int i = 0; i < 1000; i++) {
            if(i==200){
                thread.join(); //插队
            }
            System.out.println("main"+i);
        }
    }
}

16.观测线程状态(Thread.stata)

详解:
like=()->{        };
Thread thread=new Thread(like);
thread.start();
public class Demo13 {
    public static void main(String[] args) {
        //利用lam  设置Thread 的抽象方法
        Thread thread=new Thread(()->{
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                    System.out.println("/");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //观察状态
        Thread.State state=thread.getState();
        System.out.println(state);    //NEW
        //观察
        thread.start();  //启动线程
        state=thread.getState();  //启动后i的状态
        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();
            }
        }
    }
}

17.线程优先级(Priority)

1.Java提供一个线程调度器来监控程序中启动后进入就绪状态的多有线程,线程调度
器按照优先级决定因该调用哪个线程来执行.
2.线程的优先级用数字表示会 1-10;
3.使用以下方式改变或获取优先级
getPriority().setPriority(int xx)
优先级的设定建议放在start()调度前
优先级并不代表着一定先调用
public class Demo14 {
    public static void main(String[] args) {
        //1.查看主线程默认的优先级
        System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());
        MyPriority myPriority=new MyPriority();
        Thread thread_one=new Thread(myPriority);
       Thread thread_two=new Thread(myPriority);
       Thread thread_three=new Thread(myPriority);
       Thread thread_four=new Thread(myPriority);
       Thread thread_five=new Thread(myPriority);
       Thread thread_six=new Thread(myPriority);
      //设置优先级
        thread_two.setPriority(1);
        thread_three.setPriority(4);
        thread_four.setPriority(Thread.MAX_PRIORITY);
        thread_five.setPriority(8);
        thread_six.setPriority(9);
  //线程开启
        thread_one.start();
        thread_two.start();
        thread_three.start();
        thread_four.start();
        thread_five.start();
        thread_six.start();
    }
}
class MyPriority implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());
    }
}


相关文章
|
4月前
|
并行计算 调度 UED
为什么要用多线程?
为什么要用多线程?
28 0
|
7月前
|
Java API 调度
|
8月前
65.【 多线程2】(五)
65.【 多线程2】
18 0
|
7月前
|
Java
多线程
Java多线程是指在Java程序中同时运行多个线程,每个线程都是独立运行的,有自己的执行路径、栈、寄存器等资源,并且可以同步地访问共享数据。Java多线程的主要优势在于能够充分利用多核处理器,同时提升程序的并发性和响应速度。 在Java中,有多种方式可以创建和管理多线程,包括继承Thread类、实现Runnable接口和使用Executor框架等。
27 0
|
4月前
|
缓存 安全 Java
多线程05
多线程05
16 0
|
5月前
|
C#
[C#] 多线程的使用
[C#] 多线程的使用
15 0
|
9月前
|
调度
多线程 简单了解使用
多线程 简单了解使用
41 0
|
10月前
|
Linux 调度 C++
|
12月前
|
存储 缓存 算法
多线程2
多线程2https://www.nowcoder.com/issue/tutorial?tutorialId=94&uuid=b40a485f92ff496b9fe332079fa5be66
52 0
|
Linux API 调度
C++之多线程(一)
C++进阶之多线程上
101 0