• 第二十三天笔记


    黑马程序员  <a href="http://www.itheima.com" target="blank">java培训</a>

    第二十一天笔记


     1.什么是线程
        线程是程序执行的一条路径, 一个进程中可以包含多条线程
         多线程并发执行可以提高程序的效率, 可以同时完成多项工作    
    2多线程(多线程并行和并发的区别)
      并行就是两个任务同时运行,就是甲任务进行的同时,乙任务也在进行。(需要多核        CPU)
      并发是指两个任务都请求运行,而处理器只能按受一个任务,就把这两个任务安排轮 流进行,由于时间间隔较短,使人感觉两个任务都在运行。
    3多线程(Java程序运行原理和JVM的启动是多线程的吗)
      A:Java程序运行原理
         Java命令会启动java虚拟机,启动JVM,等于启动了一个应用程序,也就是启动了一个进程。该进程会自动启动一个 “主线程” ,然后主线程去调用某个类的 main 方法。
        
       B:JVM的启动是多线程的吗
         JVM启动至少启动了垃圾回收线程和主线程,所以是多线程的。
    4.多线程(多线程程序实现的方式1)
       A.继承Thread
            定义类继承Thread
            重写run方法
           把新线程要做的事写在run方法中
            创建线程对象

     开启新线程, 内部会自动执行run方法
        
            

    package lm.thread;
    
    public class Mythread  extends Thread {
        @Override
        public void run() {
             System.out.println("這是我的子程序");
        }
    }
    
    
    
    package lm.thread;
    
    public class ThreadDemo {
        public static void main(String[] args) {
        
            Mythread mth=new Mythread();
            mth.start();
        }
    
    }


         5多线程(多线程程序实现的方式2)
        实现Runnable
          定义类实现Runnable接口
           实现run方法
           把新线程要做的事写在run方法中
          创建自定义的Runnable的子类对象
          创建Thread对象, 传入Runnable
         调用start()开启新线程, 内部会自动调用Runnable的run()方法


    6.多线程(实现Runnable的原理) 查看源码
        看Thread类的构造函数,传递了Runnable接口的引用 
        通过init()方法找到传递的target给成员变量的target赋值
        查看run方法,发现run方法中有判断,如果target不为null就会调用Runnable接口子           类对象的run方法

    new Thread(new Runnable(){                                        //1,new 接口(){}实现这个接口
                public void run() {                                            //2,重写run方法
                    for(int i = 0; i < 3000; i++) {                            //3,将要执行的代码,写在run方法中
                        System.out.println("bb");
                    }
                }
            }).start(); 



    7多线程(两种方式的区别) 

    查看源码的区别:
    a.继承Thread : 由于子类重写了Thread类的run(), 当调用start()时, 直接找子类的run()方法
      b.实现Runnable : 构造函数中传入了Runnable的引用, 成员变量记住了它, start()调用run()方法时内部判断成员变量Runnable的引用是否为空, 不为空编译时看的是Runnable的run(),运行时执行的是子类的run()方法
        
    继承Thread
        好处是:可以直接使用Thread类中的方法,代码简单
         弊端是:如果已经有了父类,就不能用这种方法
     实现Runnable接口
         好处是:即使自己定义的线程类有了父类也没关系,因为有了父类也可以实现接口,而且接口是可以多实现的
        弊端是:不能直接使用Thread中的方法需要先获取到线程对象后,才能得到Thread的方法,代码复杂

    8多线程(获取名字和设置名字)

     A.获取名字
        通过getName()方法获取线程对象的名字
    B.设置名字
        通过构造函数可以传入String类型的名字
        通过setName(String)方法可以设置线程对象的名字

            Mythread1 th3=new Mythread1();
            th1.setName("lm1");
            th2.setName("lm2");
            th3.setName("lm3");
            //程序的执行顺序是随机的,和自己的电脑有关系
            th2.start();
            th3.start();
            th1.start();
        
            
            
        }
    }
    
    class Mythread1 extends Thread {
        @Override
        public void run() {
            Thread th=Thread.currentThread();
            String name=th.getName();
            if(name.equals("lm1"))
            {
                
                    test1();
            }
            if(name.equals("lm2"))
            {
                
                    test2();
            }
            if(name.equals("lm3"))
            {
                
                    test3();
    
            }
           
        }
         //线程lm1的执行代码
        private void test1()  {
        System.out.println("这是lm1的子线程");
            
        }
        //线程lm2的执行代码
        private void test2()   {
            System.out.println("这是lm2的子线程");
    
        }
        //线程lm3的执行代码
        private void test3() {
            System.out.println("这是lm3的子线程");
        }
    }


     9.多线程(获取当前线程的对象)
     Thread.currentThread(), 主线程也可以获取
      10多线程(休眠线程)
      Thread.sleep(毫秒,纳秒), 控制当前线程休眠若干毫秒1秒= 1000毫秒 1秒 = 1000 * 1000 * 1000纳秒 1000000000

    package lm.thread;
    /*
     * 程序的休眠
     */
    public class ThreadDemo5 {
        public static void main(String[] args) {
            Mythread5 th1 = new Mythread5();
            th1.start();
    
        }
    }
    
    class Mythread5 extends Thread {
        @Override
        public void run() {
             System.out.println("我要倒数啦");
             try {
                for (int i = 10; i >0; i--) {
                    System.out.println(i);
                    Thread.sleep(1000);
                }
                System.out.println("数完啦");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    11.多线程(守护线程)
      setDaemon(), 设置一个线程为守护线程, 该线程不会单独执行, 当其他非守护线程都执行结束后, 自动退出

    package lm.thread;
    
    /*
     * 关于join的演示,礼让程序
     */
    public class ThreadDemo7 {
        public static void main(String[] args) throws InterruptedException {
            Mythread8 th1 = new Mythread8();
            Mythread8 th2 = new Mythread8();
            Mythread8 th3 = new Mythread8();
            th1.setName("lm1");
            th2.setName("lm2");
            th3.setName("lm3");
            // 程序的执行顺序是随机的,和自己的电脑有关
            
            th2.start();
            th3.start();
            th3.join();
            th1.start();
            
    
        }
    }
    
    class Mythread8 extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "_" + i);
            }
        }
    }

    11.多线程(加入线程)
    join(), 当前线程暂停, 等待指定的线程执行结束后, 当前线程再继续

     join(int), 可以等待指定的毫秒之后继续

    package lm.thread;
    
    /*
     * 关于join的演示,礼让程序
     */
    public class ThreadDemo7 {
        public static void main(String[] args) throws InterruptedException {
            Mythread8 th1 = new Mythread8();
            Mythread8 th2 = new Mythread8();
            Mythread8 th3 = new Mythread8();
            th1.setName("lm1");
            th2.setName("lm2");
            th3.setName("lm3");
            // 程序的执行顺序是随机的,和自己的电脑有关
            
            th2.start();
            th3.start();
            th3.join();
            th1.start();
            
    
        }
    }
    
    class Mythread8 extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "_" + i);
            }
        }
    }


    13多线程(同步代码块)
      A.什么情况下需要同步
          当多线程并发, 有多段代码同时执行时, 我们希望某一段代码执行的过程中CPU     不要切换到其他线程工作. 这时就需要同步.
         如果两段代码是同步的, 那么同一时间只能执行一段, 在一段代码没执行结束之前, 不会执行另外一段代码.
      B.同步代码块
        使用synchronized关键字加上一个锁对象来定义一段代码, 这就叫同步代码块
          多个同步代码块如果使用相同的锁对象, 那么他们就是同步的

    class Printer {
                    Demo d = new Demo();
                    public static void print1() {
                        synchronized(d){                //锁对象可以是任意对象,但是被锁的代码需要保证是同一把锁,不能用匿名对象
                            System.out.print("黑");
                            System.out.print("马");
                            System.out.print("程");
                            System.out.print("序");
                            System.out.print("员");
                            System.out.print("
    ");
                        }
                    }
        
                    public static void print2() {    
                        synchronized(d){    
                            System.out.print("传");
                            System.out.print("智");
                            System.out.print("播");
                            System.out.print("客");
                            System.out.print("
    ");
                        }
                    }
                }


    14.多线程(同步方法)
    使用synchronized关键字修饰一个方法, 该方法中所有的代码都是同步的
    非静态同步函数的锁是:this
    静态的同步函数的锁是:字节码对象
    15.多线程(线程安全问题)
    多线程并发操作同一数据时, 就有可能出现线程安全问题
     使用同步技术可以解决这种问题, 把操作数据的代码进行同步, 不要多个线程一起操

        class Printer {
                public static void print1() {
                    synchronized(Printer.class){                //锁对象可以是任意对象,但是被锁的代码需要保证是同一把锁,不能用匿名对象
                        System.out.print("黑");
                        System.out.print("马");
                        System.out.print("程");
                        System.out.print("序");
                        System.out.print("员");
                        System.out.print("
    ");
                    }
                }
                /*
                 * 非静态同步函数的锁是:this
                 * 静态的同步函数的锁是:字节码对象
                 */
                public static synchronized void print2() {    
                    System.out.print("传");
                    System.out.print("智");
                    System.out.print("播");
                    System.out.print("客");
                    System.out.print("
    ");
                }
            }

    17_多线程(死锁)

    多线程同步的时候, 如果同步代码嵌套, 使用相同锁, 就有可能出现死

    尽量不要嵌套使用

    18_多线程(以前的线程安全的类回顾)

    线程安全 :Vector,StringBuffer,Hashtable,Collections.synchroinzed(xxx)

  • 相关阅读:
    Kubernetes 部署 Kubernetes-Dashboard v2.0.0
    Kubernetes 部署 Metrics Server 获取集群指标数据
    内网终端安全建设(转)
    内网安全运营的逻辑体系架构(转)
    thinkphp5配置文件
    MySQL索引失效的几种情况
    workman使用
    长连接技术(Long Polling)
    php好文章的记录
    php类与对象得使用场景
  • 原文地址:https://www.cnblogs.com/liman/p/4430796.html
Copyright © 2020-2023  润新知