zoukankan      html  css  js  c++  java
  • java基础——线程的常用方法和线程生命周期

    线程的常用方法

    package thread;
    /*
    测试Thread类中的常用方法:
    1.start()
    2.run():重写Thread方法,将线程要执行的操作声明在方法中
    3.Thread.currentThread():静态方法,返回执行当前代码的线程
    4.getName():获取当前线程的名字
    5.setName():设置当前线程的名字
    6.yield():当前线程交出cpu执行权
    7.join():在线程a中调用线程b的join方法,此时线程a进入阻塞态,直到线程b完全执行完后,a才结束阻塞状态
    8.stop():强制结束当期线程生命期,不推荐使用
    9.Thread.sleep(long millitime):静态方法让当前线程睡眠指定毫秒数
    10.isAlive():
    
    线程的优先级
    1.
        MAX_PRIORITY = 10
        NORM_PRIORITY = 5 默认优先级
        Min_PRIORITY = 1
    2.设置当前线程的优先级
        getPriority():获取当前线程优先级
        setPriority(int p):设置~
    
        说明:高优先级的线程要抢占低优先级现场的cpu执行权,但只是从概率上讲,并不意味着
             只有当高优先级线程执行完以后,低优先级线程才执行
    
    @author zsben
    @create 2020-01-03 10:35
    */
    
    class HelloThread extends  Thread{
        @Override
        public void run() {
            for(int i=0;i<100;i++) {
                if (i % 2 == 0) {
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ": " + i + ", "+this.getPriority());
                }
                if(i%20==0)
                    this.yield();//当前执行的线程交出执行权
            }
        }
    
        public HelloThread(String name){
            super(name);
        }
        public HelloThread(){
            super();
        }
    }
    
    public class ThreadMethodTest {
        public static void main(String[] args) {
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
            HelloThread helloThread = new HelloThread();
            //HelloThread helloThread1 = new HelloThread("thread2");
    
            helloThread.setName("thread1");
    
            helloThread.setPriority(Thread.MAX_PRIORITY);//设置优先级
            helloThread.start();
            //helloThread1.start();
    
            //给主线程命名
            Thread.currentThread().setName("main");
            for(int i=0;i<100;i++) {
                if (i % 2 == 0) {
                    System.out.println(Thread.currentThread().getName() + ": " + i);
                }
                if(i==20) {
                    try {
                        helloThread.join();//让helloThread线程先执行
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
    
            System.out.println(helloThread.isAlive());
        }
    }

    线程的生命周期

    /*

    线程生命周期: sleep(time),join()
    sleep()时间到,join()结束 |--->--------阻塞------<---| 等待同步锁,wait()
    获得同步锁,notify() | | suspend()已被废弃
    resume()已被废弃 | 获取cpu执行权 |
    新建----------------->就绪<---------------------->运行------------------->死亡
    start() 失去cpu执行权或yield() run()执行完,stop(),
    出现Error或异常且没处理

    */
  • 相关阅读:
    6大开源SIEM工具,安全信息和事件管理的“利器”
    数据库为何需要安全审计系统
    WEB漏洞扫描的开源工具
    12种开源Web安全扫描程序
    开源框架openresty+nginx 实现web应用防火墙(WAF)
    锦衣盾:开源WEB应用防火墙介绍
    20步打造最安全的Nginx Web服务器
    MySQL数据库审计系统
    数据库(分库分表)中间件对比
    Mysql调优基础、Mysql问题排查、Mysql优化、与hikari数据库连接池配合
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12148255.html
Copyright © 2011-2022 走看看