zoukankan      html  css  js  c++  java
  • JAVA多线程知识总结(二)

    本文是承接上一篇文章:JAVA多线程知识总结(一)

    四.Java多线程的阻塞状态与线程控制 

        上文已经提到线程阻塞的集中具体类型。下面主要看引起JAVA线程阻塞的方法

      1,join()-----让一个线程等待另一个线程完成之后才继续执行,如A线程的执行体中调用B线程的join方法,则A线程被阻塞,知道B线程执行完成之后,A才得以继续执行

    package com.bpan.spring.beans.thread;
    
    public class ThreadTestJoin {
        
        public static void main(String[] args) {
            
            
            MyRunnable2 myRunnable2 = new MyRunnable2();
            
            Thread thread = new Thread(myRunnable2);
            
            for( int i = 0; i < 30 ; i++) {
                
                System.out.println(Thread.currentThread().getName()+" "+i);
                
                if (i==10) {
                    
                    thread.start();
                    
                    try {
                        thread.join(); //main线程需要等待thread线程执行完毕之后再执行
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                }
                
            }
        }
    
    }
    
    
    class MyRunnable2 implements Runnable{
    
        @Override
        public void run() {
            
            for(int i = 0; i< 20;i++) {
                
                System.out.println(Thread.currentThread().getName()+" "+i);
            }
            
        }
    }

    为了查看明显的join()方法的作用,你可以将调用join()方法的代码注释掉,然后比较前后的运行结果,很明显地能够看到join()方法的作用。

      2,sleep()

      sleep —— 让当前的正在执行的线程暂停指定的时间,并进入阻塞状态。在其睡眠的时间段内,该线程由于不是处于就绪状态,因此不会得到执行的机会。即使此时系统中没有任何其他可执行的线程,出于sleep()中的线程也不会执行。因此sleep()方法常用来暂停线程执行。

    前面有讲到,当调用了新建的线程的start()方法后,线程进入到就绪状态,可能会在接下来的某个时间获取CPU时间片得以执行,如果希望这个新线程必然性的立即执行,直接调用Thread静态方法的sleep(1)即可。

    package com.bpan.spring.beans.thread;
    
    public class ThreadTestJoin {
        
        public static void main(String[] args) {
            
            
            MyRunnable2 myRunnable2 = new MyRunnable2();
            
            Thread thread = new Thread(myRunnable2);
            
            for( int i = 0; i < 30 ; i++) {
                
                System.out.println(Thread.currentThread().getName()+" "+i);
                
                /*try {
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }*/
                
                if (i==10) {
                    
                    thread.start();
                    
                    try {
    //                    thread.join(); //main线程需要等待thread线程执行完毕之后再执行
                        
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                }
                
            }
        }
    
    }
    
    
    class MyRunnable2 implements Runnable{
    
        @Override
        public void run() {
            
            for(int i = 0; i< 20;i++) {
                
                /*
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }*/
                System.out.println(Thread.currentThread().getName()+" "+i);
            }
            
        }
    }

    注:睡一个毫秒级够了,因为CPU不会空闲,会切换到新建的线程。

      3,后台线程

    概念/目的:后台线程主要是为其他线程(相对可以称之为前台线程)提供服务,或“守护线程”。如JVM中的垃圾回收线程。

    生命周期:后台线程的生命周期与前台线程生命周期有一定关联。主要体现在:当所有的前台线程都进入死亡状态时,后台线程会自动死亡(其实这个也很好理解,因为后台线程存在的目的在于为前台线程服务的,既然所有的前台线程都死亡了,那它自己还留着有什么用...伟大啊 ! !)。

    设置后台线程:调用Thread对象的setDaemon(true)方法可以将指定的线程设置为后台线程。

    package com.bpan.spring.beans.thread;
    
    public class DeamonThread {
        
        public static void main(String[] args) {
            Thread myThread = new MyThread2();
            for (int i = 0; i < 100; i++) {
                System.out.println("main thread i = " + i);
                if (i == 20) {
                    myThread.setDaemon(true);
                    myThread.start();
                }
            }
        }
        
    
    }
    
    class MyThread2 extends Thread{
    
        @Override
        public void run() {
            
            for (int i = 0; i < 100; i++) {
                System.out.println("i = " + i);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        
    }

    判断线程是否是后台线程:调用thread对象的isDeamon()方法。

    注:main线程默认是前台线程,前台线程创建中创建的子线程默认是前台线程,后台线程中创建的线程默认是后台线程。调用setDeamon(true)方法将前台线程设置为后台线程时,需要在start()方法调用之前。前天线程都死亡后,JVM通知后台线程死亡,但从接收指令到作出响应,需要一定的时间。

     4.改变线程的优先级/setPriority():

      每个线程在执行时都是具有一定的优先级的,优先级高的线程具有较多的执行机会。每个线程的优先级都与创建它的线程的优先级相同,main线程默认具有普通优先级。

      设置线程的优先级:setPriority(int priorityLevel)   

    参数priorityLevel范围在1-10之间,常用的有如下三个静态常量值:

    MAX_PRIORITY:10

    MIN_PRIORITY:1

    NORM_PRIORITY:5

    获取线程优先级:getPriority()。

    注:具有较高线程优先级的线程对象仅表示此线程具有较多的执行机会,而非优先执行。

    package com.bpan.spring.beans.thread;
    
    public class ThreadTest {
    
        
        public static void main(String[] args) {
            
            Thread thread = new MyThread5();
            
            for(int i = 0; i<100; i++) {
                
                System.out.println("main thread i= " + i);
                
                if (i == 20) {
                    
                    thread.setPriority(Thread.MAX_PRIORITY);
                    thread.start();
                }
            }
            
            
        }
    }
    
    class MyThread5 extends Thread{
    
        @Override
        public void run() {
            
            for (int i = 0; i < 100; i++) {
                System.out.println("i = " + i);
            }
    
        }
        
        
        
    }

    5.线程让步:yield()

      yield()方法的作用就是是当前线程释放占用的CPU资源,由运行状态转换为就绪状态,但是CPU调度具有随机性,下次运行的线程有可能还是调用yield()方法的线程。

      需要注意的另一点就是:yield()方法从运行状态转换到就绪状态后,CPU从就绪状态线程列表中只会选择与该线程优先级同级别或者优先级更高的线程去执行。

    package com.bpan.spring.beans.thread;
    
    public class ThreadTestMain {
    
        public static void main(String[] args) {
            Thread myThread1 = new MyThread3();
            Thread myThread2 = new MyThread4();
            myThread1.setPriority(Thread.MAX_PRIORITY);
            myThread2.setPriority(Thread.MIN_PRIORITY);
            for (int i = 0; i < 100; i++) {
                System.out.println("main thread i = " + i);
                if (i == 20) {
                    myThread1.start();
                    myThread2.start();
                    Thread.yield();
                }
            }
        }
    
    }
    
    class MyThread3 extends Thread {
    
        public void run() {
            for (int i = 0; i < 100; i++) {
                System.out.println("myThread 1 --  i = " + i);
            }
        }
    }
    
    class MyThread4 extends Thread {
    
        public void run() {
            for (int i = 0; i < 100; i++) {
                System.out.println("myThread 2 --  i = " + i);
            }
        }
    }
  • 相关阅读:
    FtpClient中文乱码问题解决
    JS点击按钮弹出窗口
    win7配置简单的FTP服务器
    docker 使用案例:部署nginx
    Asp.Net Boilerplate Project (ABP) 视频教程
    dva.js 用法总结
    webpack4: compilation.mainTemplate.applyPluginsWaterfall is not a function 解决方法
    c# MongoDB Driver 官方教程翻译
    c# redis 操作类库推荐:StackExchange.Redis.Extensions
    react-native导航器 react navigation 介绍
  • 原文地址:https://www.cnblogs.com/revel171226/p/9418099.html
Copyright © 2011-2022 走看看