zoukankan      html  css  js  c++  java
  • 测试Thread中的常用方法

     

     

    /**
    * 测试Thread中的常用方法:
    * 1. start():启动当前线程;调用当前线程的run()
    * 2. run(): 通常需要重写Thread类中的此方法,将创建的线程要执行的操作声明在此方法中
    * 3. currentThread():静态方法,返回执行当前代码的线程
    * 4. getName():获取当前线程的名字
    * 5. setName():设置当前线程的名字
    * 6. yield():释放当前cpu的执行权
    * 7. join():在线程a中调用线程b的join(),此时线程a就进入阻塞状态,直到线程b完全执行完以后,线程a才
    * 结束阻塞状态。
    * 8. stop():已过时。当执行此方法时,强制结束当前线程。
    * 9. sleep(long millitime):让当前线程“睡眠”指定的millitime毫秒。在指定的millitime毫秒时间内,当前
    * 线程是阻塞状态。
    * 10. isAlive():判断当前线程是否存活
    *
    *
    * 线程的优先级:
    * 1.
    * MAX_PRIORITY:10
    * MIN _PRIORITY:1
    * NORM_PRIORITY:5 -->默认优先级
    * 2.如何获取和设置当前线程的优先级:
    * getPriority():获取线程的优先级
    * setPriority(int p):设置线程的优先级
    *
    * 说明:高优先级的线程要抢占低优先级线程cpu的执行权。但是只是从概率上讲,高优先级的线程高概率的情况下
    * 被执行。并不意味着只有当高优先级的线程执行完以后,低优先级的线程才执行。
    *
    *
    * @author ch
    * @create 2019-02-13 下午 2:26
    */
    class HelloThread extends Thread{
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                if(i % 2 == 0){
    
    //                try {
    //                    sleep(10);
    //                } catch (InterruptedException e) {
    //                    e.printStackTrace();
    //                }
    
                    System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().getPriority() + ":" + i);
                }
    
    //            if(i % 20 == 0){
    //                yield();
    //            }
    
            }
    
        }
    
        public HelloThread(String name){
            super(name);
        }
    }
    
    
    public class ThreadMethodTest {
        public static void main(String[] args) {
    
            HelloThread h1 = new HelloThread("Thread:1");
    
    //        h1.setName("线程一");
            //设置分线程的优先级
            h1.setPriority(Thread.MAX_PRIORITY);
    
            h1.start();
    
            //给主线程命名
            Thread.currentThread().setName("主线程");
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
            for (int i = 0; i < 100; i++) {
                if(i % 2 == 0){
                    System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().getPriority() + ":" + i);
                }
    
    //            if(i == 20){
    //                try {
    //                    h1.join();
    //                } catch (InterruptedException e) {
    //                    e.printStackTrace();
    //                }
    //            }
    
            }
    
    //        System.out.println(h1.isAlive());
    
        }
    }
    
    
    
     
    
    
    /**
    *
    * 例子:创建三个窗口卖票,总票数为100张.使用继承Thread类的方式
    *
    * 存在线程的安全问题,待解决。
    *
    * @author ch
    * @create 2019-02-13 下午 4:20
    */
    package atguigu.java;
    
    /**
     *
     * 例子:创建三个窗口卖票,总票数为100张.使用继承Thread类的方式
     *
     * 存在线程的安全问题,待解决。
     *
     * @author ch
     * @create 2019-02-13 下午 4:20
     */
    class Window extends Thread{
    
    
        private static int ticket = 100;
        @Override
        public void run() {
    
            while(true){
    
                if(ticket > 0){
                    System.out.println(getName() + ":卖票,票号为:" + ticket);
                    ticket--;
                }else{
                    break;
                }
    
            }
    
        }
    }
    
    
    public class WindowTest {
        public static void main(String[] args) {
            Window t1 = new Window();
            Window t2 = new Window();
            Window t3 = new Window();
    
    
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
    
            t1.start();
            t2.start();
            t3.start();
    
        }
    }
     
    
    
    不积跬步,无以至千里;不积小流,无以成江海。
  • 相关阅读:
    redis内存模型、内存使用的优化
    【转】[Andriod]Xposed和VirtualXposed
    https详解
    http详解
    补码
    浮点数的二进制表示
    Go随机数
    ECDSA--圆锥曲线数字签名算法原理(摘wikepedia)
    Linux命令备忘
    【Windows】系统命令
  • 原文地址:https://www.cnblogs.com/CCTVCHCH/p/14642858.html
Copyright © 2011-2022 走看看