• 多线程 小例子


    1. 线程小例子
     /**
     * 失去一日甚易,欲得回已无途!
     *                     2016-11-17下午1:43:51
     * 
     * 进程:可以包含一个或者多个线程!  cpu上  真正 执行的 是 线程!
     * 
     *   实现多线程的方法:
     *    01.继承Thread类            重写run()
     *    02.实现Runnable接口   重写run()
     * 
     * 多线程:在单个程序中,同时运行多个线程来完成不同的工作! 多个线程   并发   执行的技术!
     * 
     * 并发:在同一个时间段中 有几个程序 都处于 启动运行 到 运行完毕 之间!
     *      计算机同时运行几个程序,或者运行同一个程序的多个进程或者线程!
     *      各就各位  ,预备!  跑!
     *         运动员的状态???   并发!               
     *       秋季 是感冒的高发期!   
     *  
     * 在一个确定的事件点! 只能有一个线程在执行!                   
     *                     
     * run()和start()区别:
     *      run():就是类中的一个普通方法,如果两个类中都有run(),那么肯定是一个run()执行完毕之后才会执行另一个run();
     *      修饰符必须是public! 返回值类型 必须是 void!           
     *      
     *      start():只有执行start()才算是启动线程!这是一个就绪状态!默认会执行run();无需等待run()中的内容全部执行完毕!  
     *       在调用start()并没有立即执行run()!而是在一个不确切的时间执行!cpu分配时间片给当前线程时,
     *       当前线程才执行run()里面的代码!
     * 
     * 线程的生命周期:
     * 1.新生
     * Thread01 thread01=new Thread01();
     * 2.就绪
     *  thread01.start();        
     * 3.运行
     *  cpu分配时间片给thread01的时候,执行run()
     * 4.阻塞
     *  sleep(); wait()
     * 5.死亡
     *   001.正常    run()执行完毕后   正常的退出
     *   002.异常  run()执行的过程中,出现异常情况,非正常的退出!
     *   
     *线程的优先级!
     *   默认值  是 5,最小值 1, 最大值  10!
     *   thread01.setPriority(9);
     */
    public class MyThread {
        public static void main(String[] args) {
            //创建第一个线程  继承了 Thread
            Thread01 thread01=new Thread01();
            thread01.setName("当前线程--1");
            thread01.start();
            System.out.println("**************");
            //创建第二个线程   实现了Runnable
            Thread thread02=new Thread(new Thread02());
            thread02.setPriority(9);  //设置优先级
            thread02.setName("当前线程--2");
            thread02.start();
            
        }
    
    }
    
    //01. 继承Thread  重写run()
    class  Thread01  extends Thread{
         private  int  count;
         //输出0-100之间的偶数和
         public void run(){
             while (count<=100) {
                if (count%2==0) {
                    // 获取当前正在执行的线程  Thread.currentThread()
                    System.out.println(Thread.currentThread().getName()+":"+count);
                }
                count++;
            }
         }
    }
    
    
    //02. 实现Runnable接口 重写run()
    class  Thread02 implements Runnable{
         private  int  count;
         //输出0-100之间的偶数和
         public void run(){
             while (count<=100) {
                if (count%2==0) {
                    // 获取当前正在执行的线程  Thread.currentThread()
                    System.out.println(Thread.currentThread().getName()+":"+count);
                }
                count++;
            }
         }
    }

       

     2.同步代码方法 实现卖票
    public class SaleTicket implements Runnable{
         //库存票数
         private  int   tickets=100;
        
        /*
         * 卖票  同步代码方法 比 同步代码块 性能高!  但是功能是一致的!
         * 一个类中可以有多个 同步方法 或者同步代码块
         * 
         * 当 一个对象调用这个对象中的同步方法时!  还可以同时调用其他的同步方法!  无需等待!
         * 但是  一个对象执行这个对象中的同步代码块! 这个对象中的所有同步代码块 都上锁了! 都必须等待!
         * 
         * 同步代码块 锁的是 对象  并不是 代码块!
         * 
         */
         private synchronized  void  sale(){
             if (tickets>0) {
                System.out.println(Thread.currentThread().getName()+"卖出了"+tickets+"票");
                //票数-1
                tickets--;
            }
             try {
                Thread.sleep(2);  //让cpu重新分配时间片
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
         }
    
        @Override
        public void run() {
             while (tickets>0) {
                sale();
            }
        }
    
        public static void main(String[] args) {
            SaleTicket ticket=new SaleTicket();
            Thread t1=new Thread(ticket);
            Thread t2=new Thread(ticket);
            Thread t3=new Thread(ticket);
            Thread t4=new Thread(ticket);
            //设置窗口名称
            t1.setName("1号窗口");
            t2.setName("2号窗口");
            t3.setName("3号窗口");
            t4.setName("4号窗口");
            //开始卖票
            t1.start();
            t2.start();
            t3.start();
            t4.start();
            
        }  
    }

       

     3.同步代码块方法实现卖票
    public class SaleTicket implements Runnable{
         //库存票数
         private  int   tickets=100;
        
        /*
         * 但是  一个对象执行这个对象中的同步代码块! 这个对象中的所有同步代码块 都上锁了! 都必须等待!
         * 
         * 同步代码块 锁的是 对象  并不是 代码块!
         * 
         */
    publicclass SaleTicket implements Runnable{
        private int count = 100;//库存票数
        @Override
     public void run() {
       while (count > 0) {
       //同步代码块
        synchronized (this) {
         System.out.println(Thread.currentThread().getName() + "卖出了第"
           + count + "张票。");
         count--;
        }
        try {
         Thread.sleep(2);
        } catch (InterruptedException e) {
         e.printStackTrace();
        }
       }
     } public static void main(String[] args) { SaleTicket ticket=new SaleTicket(); Thread t1=new Thread(ticket); Thread t2=new Thread(ticket); Thread t3=new Thread(ticket); Thread t4=new Thread(ticket); //设置窗口名称 t1.setName("1号窗口"); t2.setName("2号窗口"); t3.setName("3号窗口"); t4.setName("4号窗口"); //开始卖票 t1.start(); t2.start(); t3.start(); t4.start(); } }

       

    4.小总结
     /*
    Runnable Thread Callable Future 多线程 Runnable 与 Callable 区别
    01.Runnable默认规定执行的方法时run();Callable默认规定执行的方法时call();
    02.Runnable的run()没有返回值,Callable中的call()具有返回值
    03.run()不能声明异常!必须内部处理! call()可以声明异常
    04.运行Callable的时候可以 拿到一个Future对象 !
     
    Future是一个模式!
    核心思想====》 原本需要等待的时间段,现在可以去做一些其他的业务逻辑!
    假设 现在有一个 具有返回值的A() 还有一个B() 和C()
    之前我们调用A() 必须等待A()返回结构之后 才能调用 B()或者C()
    现在我们调用A() 不须等待A()返回结构之后 也能调用 B()或者C() Future
    */

       

    5.验证同步方法的锁
    public class A implements Runnable {
    
        //这个方法  其他 线程也可以进入
       public synchronized  void  test(){
           System.out.println("开始......");
           try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
           System.out.println("结束......");
       
       } 
        
        @Override
        public void run() {
            test();
        } 
        
        public static void main(String[] args) {
            for (int i = 1; i <=5; i++) {
                Thread thread=new Thread(new A());
                thread.start();
            }          
        }
    
    }
     
     
     
     
     
     
     

       

    验证同步方法的锁
    6.验证同步代码块的锁
    public class A implements Runnable {
    
        //验证同步代码库锁的是 当前对象
         public   void  test(){
                synchronized (this) {
                       System.out.println("开始......");
                       try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                       System.out.println("结束......");
                }
               
               }
        
        @Override
        public void run() {
            test();
        }
        
        
        public static void main(String[] args) {
            A a=new A();
            for (int i = 1; i <=5; i++) {
                Thread thread=new Thread(a);
                thread.start();
            }
        }
    }
  • 相关阅读:
    java中equals与hashCode的重写问题
    关于java的二维码的生成与解析
    bootstrap的时间控件使用(双日历)
    Mysql表,列,库的增删查改
    关于js重名方法的先后调用问题
    javascript的比较运算符
    javascript的运算符
    JavaScript的数据类型
    javascript的基本语法
    MAP集合
  • 原文地址:https://www.cnblogs.com/HHR-SUN/p/7039291.html
Copyright © 2020-2023  润新知