zoukankan      html  css  js  c++  java
  • join方法

    测试案例

    ------------------------------------------------------------测试类------------------------------------------------------------

     1 package com.qf.test;
     2 
     3 /**
     4  * @author qf
     5  * @create 2018-09-20 15:32
     6  */
     7 public class Run {
     8     static int count = 0;
     9     public static void main(String[] args) {
    10         Thread thread = new Thread(new Runnable() {
    11             @Override
    12             public void run() {
    13                 for (int i = 0; i < 10; i++) {
    14                     count++;
    15                 }
    16             }
    17         });
    18         thread.start();
    19         System.out.println("count = " + count);
    20     }
    21 }

    -----------------------------------------------------------打印结果-----------------------------------------------------------

    count = 0

    结果是0,并没有被修改成10,说明main线程在thread线程之前执行完成了

    如何获取修改后的count值?使用join方法

    ------------------------------------------------------------测试类------------------------------------------------------------

     1 package com.qf.test;
     2 
     3 /**
     4  * @author qf
     5  * @create 2018-09-20 15:32
     6  */
     7 public class Run {
     8     static int count = 0;
     9     public static void main(String[] args) throws InterruptedException {
    10         Thread thread = new Thread(new Runnable() {
    11             @Override
    12             public void run() {
    13                 for (int i = 0; i < 10; i++) {
    14                     count++;
    15                 }
    16             }
    17         });
    18         thread.start();
    19         thread.join();
    20         System.out.println("count = " + count);
    21     }
    22 }

    -----------------------------------------------------------打印结果-----------------------------------------------------------

    count = 10

    原因分析

    join源码

        
        public final void join() throws InterruptedException {
            join(0);
        }
    ==============================================================================
        public final synchronized void join(long millis)
            throws InterruptedException {
            long base = System.currentTimeMillis();
            long now = 0;
    
            if (millis < 0) {
                throw new IllegalArgumentException("timeout value is negative");
            }
    
            if (millis == 0) {
                while (isAlive()) {
                    wait(0);
                }
            } else {
                while (isAlive()) {// thread对象调用的isAlive方法,所以只要thread还存活,就会循环
                    long delay = millis - now;
                    if (delay <= 0) {
                        break;
                    }
                    wait(delay);// wait方法,使当前线程等待,当前线程是main线程
                    now = System.currentTimeMillis() - base;
                }
            }
        }

    join方法使得当前线程进入阻塞状态,进入排队队列的作用,且必须等待调用join的线程对象执行完run方法才能执行

  • 相关阅读:
    zoj 3620 Escape Time II dfs
    zoj 3621 Factorial Problem in Base K 数论 s!后的0个数
    bzoj 1789: [Ahoi2008]Necklace Y型项链 贪心
    BZOJ 4027: [HEOI2015]兔子与樱花 树上dp
    Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 分块
    图论:单源最短路与多源最短路问题
    图论:图的概念与图的储存方式
    hdu 4802 GPA 水题
    Codeforces Round #202 (Div. 1) A. Mafia 贪心
    Spring Bean配置默认为单实例 pring Bean生命周期
  • 原文地址:https://www.cnblogs.com/qf123/p/9682107.html
Copyright © 2011-2022 走看看