zoukankan      html  css  js  c++  java
  • java学习之- 线程运行状态

    标签(空格分隔): 线程运行状态


    线程的运行状态:

    如下是是我编写的一个图,大家可以作为参考:
    image.png-296.1kB
    1.new一个thread子类也是创建了一个线程;
    2.创建完毕之后start()-----运行,
    3.然后从运行-------sleep(time)----冻结----sleep(time)时间到了,恢复到运行;
    4.还有一种,是线程运行的时候------wait时间------到冻结状态-------然后notify()唤醒-----然后恢复到运行状态;
    5.消亡:线程运行中/冻结:stop()/run方法结束---------消亡;
    6.临时状态:当前状态等待CPU的执行权,具备运行资格没有执行权,
    7.冻结:放弃了执行资格;
    8.冻结状态-----恢复---临时状态/运行状态;

    获取线程的对象和名称:

    创建了对象之后,都要匹配一个名称,线程的名称都要定义在线程的事物里面:

    class Demo extends Thread{
        private String name;
        Demo(String name){
            this.name=name;
        }
        public void run(){//将运行的代码放在run方法中
            for(int x=0;x<60;x++) {
                System.out.println(this.getName()+"demo run-----"+x);
            }
        }
    }
    class ThreadDemo{
    
        public static void main(String[] args){
            Demo d1= new Demo("one");//创建好一个线程
            Demo d2= new Demo("two");
            d1.start();
            d2.start();
            for(int x=0;x<60;x++){
                System.out.println("helloworld!----"+x);
            }
        }
    }
    
    • 原来线程都有自己默认的名称,Thread-编号,该编号从零开始;
      2.上述的名称是不是不太好看,这里可以使用set方法来定义名称;

    获取当前线程的名称:

    • static Thread currentThread():获取当前线程的对象;
      getName():获取线程名称;
      设置线程名称:setName或者构造方法;

    练习:

    • 需求是一个简单的卖票程序,多个窗口卖票,1号窗口在卖的时候,2号也在卖;多个窗口同时卖票,

    观察如下的程序:

    class Ticket extends Thread{
        private int tick=100;
        public void run(){
            while (true){
                if(tick>0) {
                    System.out.println(Thread.currentThread().getName()+"sale:" + tick--);
                }
            }
        }
    
    
    }
    class TickDemo{
        public static void main(String[] args){
            Ticket t1=new Ticket();
            Ticket t2=new Ticket();
            Ticket t3=new Ticket();
            Ticket t4=new Ticket();
            t1.start();
            t2.start();
            t3.start();
            t4.start();
        }
    }
    
    

    执行结果:
    发现100个票,卖出去了400张票;每个对象里面包含了100张票;

    如何实现:100张票4个窗口正常售卖呢?,我们可以通过static定义变量来实现,如下

    class Ticket extends Thread{
        private static int tick=100;
        public void run(){
            while (true){
                if(tick>0) {
                    System.out.println(Thread.currentThread().getName()+"sale:" + tick--);
                }
            }
        }
    
    
    }
    class TickDemo{
        public static void main(String[] args){
            Ticket t1=new Ticket();
            Ticket t2=new Ticket();
            Ticket t3=new Ticket();
            Ticket t4=new Ticket();
            t1.start();
            t2.start();
            t3.start();
            t4.start();
        }
    }
    
    

    执行通过;

    • 但是一般我们不使用静态,因为静态的生命周期比较长;
    • 我们使用Thread创建线程不能实现了;

    runable接口:

    • Runable接口应该由那些打算通过某一线程执行实例的类来实现,类必须顶一个称为run的无参数方法,设计该接口的目的就是提供一个公共协议,例如,Thread类实现了Runable,激活的意思是某个线程已经启动并且尚未停止;
    
    class Ticket implements Runnable{
        private static int tick=100;
        public void run(){
            while (true){
                if(tick>0) {
                    System.out.println(Thread.currentThread().getName()+"sale:" + tick--);
                }
            }
        }
    
    
    }
    class TickDemo{
        public static void main(String[] args){
    //        Ticket t1=new Ticket();
    //        Ticket t2=new Ticket();
    //        Ticket t3=new Ticket();
    //        Ticket t4=new Ticket();
    //        t1.start();
    //        t2.start();
    //        t3.start();
    //        t4.start();
            Ticket t=new Ticket();
            Thread t1=new Thread();
            Thread t2=new Thread();
            Thread t3=new Thread();
            Thread t4=new Thread();
            t1.start();
            t2.start();
            t3.start();
            t4.start();
    
    
    
        }
    }
    

    上述代码:能否实现呢?
    分析:t1调用运行的run()是Thread里面的run,在这次代码里面未重写;

    package com.wangaling;
    class Ticket implements Runnable{
        private static int tick=100;
        public void run(){
            while (true){
                if(tick>0) {
                    System.out.println(Thread.currentThread().getName()+"sale:" + tick--);
                }
            }
        }
    
    
    }
    class TickDemo{
        public static void main(String[] args){
    //        Ticket t1=new Ticket();
    //        Ticket t2=new Ticket();
    //        Ticket t3=new Ticket();
    //        Ticket t4=new Ticket();
    //        t1.start();
    //        t2.start();
    //        t3.start();
    //        t4.start();
            Ticket t=new Ticket();
            Thread t1=new Thread(t);
            Thread t2=new Thread(t);
            Thread t3=new Thread(t);
            Thread t4=new Thread(t);
            t1.start();
            t2.start();
            t3.start();
            t4.start();
    
    
    
        }
    }
    
    

    执行结果:
    上述代码可以正常实现售票的功能;

    创建线程的第二种方式,实现runnable接口
    步骤1:
    1。定义类实现Runable接口;
    2.覆盖Runable接口中的run方法:将线程要运行的代码存在在run方法中
    3.通过Thread类建立线程对象
    4.将Runable接口的子类对象作为实际参数传递给Thread类的构造函数:为什么要将Runable接口的子类的对象传递给Thread的构造函数,因为,自定义的run方法所属的对象是Runable接口的子类对象,所以要让线程去指定指定对象的run方法,就必须明确该run方法所属的对象;
    5.调用Thread类的start方法开启线程,并调用Runable接口子类的run方法;

    实现方式和继承方式有很么区别?

    • 1.实现方式好处:避免了单继承的局限性,在定义线程的时候建议是用实现方式,可以避免单继承的实现性;

    两种方式区别:

    • 继承Thread:线程代码存放在Thread子类的run方法中;
      实现Runable,线程代码存在接口的子类的run方法

    上述程序的分析:

    1。通过分析发现:打印票会出现:0,-1,-2等错票;

    多线程的运行出现了安全问题:

    • 问题的原因:
      当多条语句在操作同一个线程共享数据时,一个线程对多条语句执行了一部分还没有执行完另一个线程已经参与进来了,导致了共享数据的错误
      解决办法:
      对多条操作共享数据的语句,只能让一个线程都执行完毕,在执行过程中,其他线程不可以执行;(就是说拿到了执行权也不让他执行)
    • java对多线程提供了专业的解决方式,就是同步代码块,
      synchronized(对象){
      需要被同步的代码块,怎么判断哪些代码块需要同步,就看谁操作了共享数据;
      }
      对象如同锁,持有锁的线程可以在同步中执行,没有持有锁的线程即使获得了CPU的执行权限,也进不去,因为没有获取锁;--理解这个概念可以理解火车上的卫生间;
    • 同步的前提:
      1.必须要有2个或者2个以上的线程;
      2.必须多个线程使用一个锁;
      必须保证同步中只有一个线程运行,有些需要同步,有些不需要同步;
      好处:解决多线程的安全问题;
      弊端:每个线程在获取到CPU权限之后,每次都要判断一下锁,这样使程序会变得非常的慢;
    package com.wangaling;
    class Ticket implements Runnable{
        private static int tick=100;
        Object obj=new Object();
        public void run(){
            while (true){
                synchronized (obj){
                    if (tick > 0) {
                    try(Thread.sleep(10);)catch(Exception e){}
                        System.out.println(Thread.currentThread().getName() + "sale:" + tick--);
                    }
                }
            }
        }
    }
    class TickDemo{
        public static void main(String[] args){
    //        Ticket t1=new Ticket();
    //        Ticket t2=new Ticket();
    //        Ticket t3=new Ticket();
    //        Ticket t4=new Ticket();
    //        t1.start();
    //        t2.start();
    //        t3.start();
    //        t4.start();
            Ticket t=new Ticket();
            Thread t1=new Thread(t);
            Thread t2=new Thread(t);
            Thread t3=new Thread(t);
            Thread t4=new Thread(t);
            t1.start();
            t2.start();
            t3.start();
            t4.start();
        }
    }
    
    /*
    需求:
    银行有一个金库,有两个储户分别存300,每次存100,存3次。
    目的:改程序是否有安全问题,如果有,如何解决?
    
    如何找问题:
    1.明确哪些代码是多线程运行的代码
    2.明确共享数据
    3.明确多线程运行代码中哪些语句是操作共享数据的。
     */
    
    class Bank{
        private int sum;
        Object obj=new Object();
        public void add(int n ){
            synchronized (obj){
                sum=sum+n;
    
                try{Thread.sleep(10);}
                catch(Exception e){};
                System.out.println("sum="+sum);
            }
    
        }
    
    }
    class Cus implements Runnable{
        private Bank b=new Bank();
        public void run(){
    
            for(int x=0;x<3;x++){
    
                b.add(100);
            }
        }
    
    }
    class BankDemo{
        public static void main(String[] args){
    
            Cus c=new Cus();
            Thread t1=new Thread(c);
            Thread t2=new Thread(c);
            t1.start();
            t2.start();
        }
    
    

    如上述的需求我们可以修改为同步函数:

    class Bank{
        private int sum;
    //    Object obj=new Object();
        public synchronized void add(int n ){
    //        synchronized (obj){
                sum=sum+n;
    
                try{Thread.sleep(10);}
                catch(Exception e){};
                System.out.println("sum="+sum);
    //        }
    
        }
    
    }
    class Cus implements Runnable{
        private Bank b=new Bank();
        public void run(){
    
            for(int x=0;x<3;x++){
    
                b.add(100);
            }
        }
    
    }
    class BankDemo{
        public static void main(String[] args){
    
            Cus c=new Cus();
            Thread t1=new Thread(c);
            Thread t2=new Thread(c);
            t1.start();
            t2.start();
        }
    
    
    • 问题通过上边的学习我们可以对:函数进行同步,那如下同步可以吗?
    class Ticket implements Runnable{
        private int num=1000;
        public synchronized  void run(){
            while(true) {
                if(num>0){
    
                    try{
    
                        Thread.sleep(10);
                    }catch (Exception e){}
                    System.out.println(Thread.currentThread().getName()+"sale"+num--);
                }
    
            }
        }
    }
    class ThisLockDemo{
        public static void main(String[] args){
    
            Ticket t=new Ticket();
            Thread t1= new Thread(t);
            Thread t2=new Thread(t);
            Thread t3=new Thread(t);
            t1.start();
            t2.start();
            t3.start();
            
        }
    }
    
    • 答案不可以,我们可以把run方法体抽离出来单独写一个函数,然后在run里面调用就可以了
    class Ticket implements Runnable{
        private int num=1000;
        public void run(){
            while(true){
            show();}
    }
    public synchronized  void show(){
            if(num>0){
                try{
                    Thread.sleep(10);
                }catch (Exception e){}
                System.out.println(Thread.currentThread().getName()+"sale"+num--);
            }
    }
    }}
    class ThisLockDemo{
        public static void main(String[] args){
    
            Ticket t=new Ticket();
            Thread t1= new Thread(t);
            Thread t2=new Thread(t);
            Thread t3=new Thread(t);
            t1.start();
            t2.start();
            t3.start();
        }
    }
    
    • 上述的例子:同步函数用的哪锁呢?
      1.函数需要被对象调用,函数都有一个所属对象的引用,就是this,所以同步函数使用的就是this;
      2.通过该程序进行验证,使用两个线程进行卖票,一个线程在同步代码块中,一个在同步函数中,都在执行卖票程序,如果同步不会出现错误的票;
    class Ticket implements Runnable{
        private int num=1000;
        Object obj =new Object();
        boolean flag =true;
        public void run(){
        if (flag){
            while(true){
            synchronized(this){
             if(num>0){
                try{
                    Thread.sleep(10);
                }catch (Exception e){}
                System.out.println(Thread.currentThread().getName()+"sale"+num--);
            }
            }
            this.show();}
    }
    }else{
        while (true){
        show();
        }
    }
    public synchronized  void show(){
            if(num>0){
                try{
                    Thread.sleep(10);
                }catch (Exception e){}
                System.out.println(Thread.currentThread().getName()+"sale"+num--);
            }
    }
    }}
    class ThisLockDemo{
        public static void main(String[] args){
    
            Ticket t=new Ticket();
            Thread t1= new Thread(t);
            Thread t2=new Thread(t);
         
            t1.start();
            t1.flag=true;
            t2.start();
        }
    }
    
    • 如果同步函数被静态修饰后,使用的锁是什么呢?
      通过发现,不是在是this,因为静态方法也不可以定义this,静态进内存是,内存中没有本类对象,但是一定有该类对应额字节码文件对象;
      类名,class,该对象的类型是Class;
      静态同步方法,使用的锁是该方法,所在类字节码文件对象,类名class;
    class Ticket implements Runnable{
        private int num=1000;
        boolean flag =true;
        public void run(){
        if (flag){
            while(true){
            synchronized(Ticket.class){
             if(num>0){
                try{
                    Thread.sleep(10);
                }catch (Exception e){}
                System.out.println(Thread.currentThread().getName()+"sale"+num--);
            }
            }
            this.show();}
    }
    }else{
        while (true){
        show();
        }
    }
    public synchronized  void show(){
            if(num>0){
                try{
                    Thread.sleep(10);
                }catch (Exception e){}
                System.out.println(Thread.currentThread().getName()+"sale"+num--);
            }
    }
    }}
    class ThisLockDemo{
        public static void main(String[] args){
    
            Ticket t=new Ticket();
            Thread t1= new Thread(t);
            Thread t2=new Thread(t);
         
            t1.start();
            t1.flag=true;
            t2.start();
        }
    }
    

    死锁:

    同步中嵌套同步,而锁确不同;

    class Ticket implements Runnable{
        private int num=1000;
        Object obj=new Object();
        boolean flag =true;
        public void run(){
        if (flag){
            while(true){
            synchronized(obj){
             if(num>0){
                try{
                    Thread.sleep(10);
                }catch (Exception e){}
                System.out.println(Thread.currentThread().getName()+"sale"+num--);
            }
            }
            this.show();}
    }
    }else{
        while (true){
        show();
        }
    }
    public synchronized  void show(){
        synchronized(obj){
            if(num>0){
                try{
                    Thread.sleep(10);
                }catch (Exception e){}
                System.out.println(Thread.currentThread().getName()+"sale"+num--);
            }
    }
    }}
    
    class ThisLockDemo{
        public static void main(String[] args){
    
            Ticket t=new Ticket();
            Thread t1= new Thread(t);
            Thread t2=new Thread(t);
         
            t1.start();
            t1.flag=true;
            t2.start();
        }
    }
    
  • 相关阅读:
    298. Binary Tree Longest Consecutive Sequence
    117. Populating Next Right Pointers in Each Node II
    116. Populating Next Right Pointers in Each Node
    163. Missing Ranges
    336. Palindrome Pairs
    727. Minimum Window Subsequence
    211. Add and Search Word
    年底购物狂欢,移动支付安全不容忽视
    成为程序员前需要做的10件事
    全球首推iOS应用防破解技术!
  • 原文地址:https://www.cnblogs.com/surewing/p/11427518.html
Copyright © 2011-2022 走看看