zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然java开发常用类库学习笔记:线程常用的操作方法

    class MyThread implements Runnable{    // 实现Runnable接口
        public void run(){    // 覆写run()方法
            for(int i=0;i<3;i++){
                System.out.println(Thread.currentThread().getName()
                        + "运行,i = " + i) ;    // 取得当前线程的名字
            }
        }
    };
    public class CurrentThreadDemo{
        public static void main(String args[]){
            MyThread mt = new MyThread() ;    // 实例化Runnable子类对象
            new Thread(mt,"线程").start() ;        // 启动线程
            mt.run() ;    // 直接调用run()方法
        }
    };
    public class MainPriorityDemo{
        public static void main(String args[]){
            System.out.println("主方法的优先级:" + 
                Thread.currentThread().getPriority()) ;    // 取得主方法的优先级
            System.out.println("MAX_PRIORITY = " + Thread.MAX_PRIORITY) ;
            System.out.println("NORM_PRIORITY = " + Thread.NORM_PRIORITY) ;
            System.out.println("MIN_PRIORITY = " + Thread.MIN_PRIORITY) ;
        }
    };
    class MyThread implements Runnable{    // 实现Runnable接口
        public void run(){    // 覆写run()方法
            for(int i=0;i<3;i++){
                System.out.println(Thread.currentThread().getName()
                        + "运行,i = " + i) ;    // 取得当前线程的名字
            }
        }
    };
    public class ThreadAliveDemo{
        public static void main(String args[]){
            MyThread mt = new MyThread() ;    // 实例化Runnable子类对象
            Thread t = new Thread(mt,"线程");        // 实例化Thread对象
            System.out.println("线程开始执行之前 --> " + t.isAlive()) ;     // 判断是否启动
            t.start() ;    // 启动线程
            System.out.println("线程开始执行之后 --> " + t.isAlive()) ;     // 判断是否启动
            for(int i=0;i<3;i++){
                System.out.println(" main运行 --> " + i) ;
            }
            // 以下的输出结果不确定
            System.out.println("代码执行之后 --> " + t.isAlive()) ;     // 判断是否启动
            
        }
    };
    class MyThread implements Runnable{    // 实现Runnable接口
        public void run(){    // 覆写run()方法
            while(true){
                System.out.println(Thread.currentThread().getName() + "在运行。") ;
            }
        }
    };
    public class ThreadDaemonDemo{
        public static void main(String args[]){
            MyThread mt = new MyThread() ;    // 实例化Runnable子类对象
            Thread t = new Thread(mt,"线程");        // 实例化Thread对象
            t.setDaemon(true) ;    // 此线程在后台运行
            t.start() ;    // 启动线程
        }
    };
    class MyThread implements Runnable{    // 实现Runnable接口
        public void run(){    // 覆写run()方法
            System.out.println("1、进入run()方法") ;
            try{
                    Thread.sleep(10000) ;    // 线程休眠10秒
                    System.out.println("2、已经完成了休眠") ;
            }catch(InterruptedException e){
                System.out.println("3、休眠被终止") ;
                return ; // 返回调用处
            }
            System.out.println("4、run()方法正常结束") ;
        }
    };
    public class ThreadInterruptDemo{
        public static void main(String args[]){
            MyThread mt = new MyThread() ;    // 实例化Runnable子类对象
            Thread t = new Thread(mt,"线程");        // 实例化Thread对象
            t.start() ;    // 启动线程
            try{
                    Thread.sleep(2000) ;    // 线程休眠2秒
            }catch(InterruptedException e){
                System.out.println("3、休眠被终止") ;
            }
            t.interrupt() ;    // 中断线程执行
        }
    };
    class MyThread implements Runnable{    // 实现Runnable接口
        public void run(){    // 覆写run()方法
            for(int i=0;i<50;i++){
                System.out.println(Thread.currentThread().getName()
                        + "运行,i = " + i) ;    // 取得当前线程的名字
            }
        }
    };
    public class ThreadJoinDemo{
        public static void main(String args[]){
            MyThread mt = new MyThread() ;    // 实例化Runnable子类对象
            Thread t = new Thread(mt,"线程");        // 实例化Thread对象
            t.start() ;    // 启动线程
            for(int i=0;i<50;i++){
                if(i>10){
                    try{
                        t.join() ;    // 线程强制运行
                    }catch(InterruptedException e){}
                }
                System.out.println("Main线程运行 --> " + i) ;
            }
        }
    };
    class MyThread implements Runnable{    // 实现Runnable接口
        public void run(){    // 覆写run()方法
            for(int i=0;i<3;i++){
                System.out.println(Thread.currentThread().getName()
                        + "运行,i = " + i) ;    // 取得当前线程的名字
            }
        }
    };
    public class ThreadNameDemo{
        public static void main(String args[]){
            MyThread mt = new MyThread() ;    // 实例化Runnable子类对象
            new Thread(mt).start() ;        // 系统自动设置线程名称
            new Thread(mt,"线程-A").start() ;        // 手工设置线程名称
            new Thread(mt,"线程-B").start() ;        // 手工设置线程名称
            new Thread(mt).start() ;        // 系统自动设置线程名称
            new Thread(mt).start() ;        // 系统自动设置线程名称
        }
    };
    class MyThread implements Runnable{    // 实现Runnable接口
        public void run(){    // 覆写run()方法
            for(int i=0;i<5;i++){
                try{
                        Thread.sleep(500) ;    // 线程休眠
                }catch(InterruptedException e){}
                System.out.println(Thread.currentThread().getName()
                        + "运行,i = " + i) ;    // 取得当前线程的名字
            }
        }
    };
    public class ThreadPriorityDemo{
        public static void main(String args[]){
            Thread t1 = new Thread(new MyThread(),"线程A") ;    // 实例化线程对象
            Thread t2 = new Thread(new MyThread(),"线程B") ;    // 实例化线程对象
            Thread t3 = new Thread(new MyThread(),"线程C") ;    // 实例化线程对象
            t1.setPriority(Thread.MIN_PRIORITY) ;    // 优先级最低
            t2.setPriority(Thread.MAX_PRIORITY) ;    // 优先级最低
            t3.setPriority(Thread.NORM_PRIORITY) ;    // 优先级最低
            t1.start() ;    // 启动线程
            t2.start() ;    // 启动线程
            t3.start() ;    // 启动线程
        }
    };
    class MyThread implements Runnable{    // 实现Runnable接口
        public void run(){    // 覆写run()方法
            for(int i=0;i<50;i++){
                try{
                        Thread.sleep(500) ;    // 线程休眠
                }catch(InterruptedException e){}
                System.out.println(Thread.currentThread().getName()
                        + "运行,i = " + i) ;    // 取得当前线程的名字
            }
        }
    };
    public class ThreadSleepDemo{
        public static void main(String args[]){
            MyThread mt = new MyThread() ;    // 实例化Runnable子类对象
            Thread t = new Thread(mt,"线程");        // 实例化Thread对象
            t.start() ;    // 启动线程
        }
    };
    class MyThread implements Runnable{    // 实现Runnable接口
        public void run(){    // 覆写run()方法
            for(int i=0;i<5;i++){
                try{
                    Thread.sleep(500) ;
                }catch(Exception e){}
                System.out.println(Thread.currentThread().getName()
                        + "运行,i = " + i) ;    // 取得当前线程的名字
                if(i==2){
                    System.out.print("线程礼让:") ;
                    Thread.currentThread().yield() ;    // 线程礼让
                }
            }
        }
    };
    public class ThreadYieldDemo{
        public static void main(String args[]){
            MyThread my = new MyThread() ;    // 实例化MyThread对象
            Thread t1 = new Thread(my,"线程A") ;
            Thread t2 = new Thread(my,"线程B") ;
            t1.start() ;
            t2.start() ;
        }
    };
  • 相关阅读:
    Linux网卡设置
    Linux虚拟机-----概述(1)
    Redis缓存数据库-----概述(1)
    Spring和Mybatis的集成
    onehot编码解释
    LINUX-CUDA版本所对应的NVIDIA驱动版本号,cuda版本报错的朋友参考一下
    matplotlib画图
    pytorch实现花朵数据集读取
    轻量架构ShuffleNet V2:从理论复杂度到实用设计准则
    CBAM: 卷积块注意模块
  • 原文地址:https://www.cnblogs.com/tszr/p/12152836.html
Copyright © 2011-2022 走看看