• java多线程 基础demo


    join()
     
    让主进程等待子进程全部执行完
    例子如下:
     
    package mocker;
    public class TestThread5 extends Thread {
         private String name;
         public TestThread5(String name) {
              super(name);
              this.name = name;
         }
         @Override
         public void run() {
              System.out.println(Thread.currentThread().getName() + "线程运行开始 ");
              for (int i = 0; i < 5; i++) {
                  System.out.println("子线程" + name + "运行: " + i);
                  try {
                       sleep((int) Math.random() * 10);
                  } catch (InterruptedException e) {
                       e.printStackTrace();
                  }
              }
              System.out.println(Thread.currentThread().getName() + "线程运行结束");
         }
         public static void main(String[] args) {
              System.out.println(Thread.currentThread().getName() + "主线程运行开始!");
              TestThread5 mTh1 = new TestThread5("A");
              TestThread5 mTh2 = new TestThread5("B");
              mTh1.start();
              mTh2.start();
              try{
                  mTh1.join();
              }catch(InterruptedException e){
                  e.printStackTrace();
              }
              try{
                  mTh2.join();
              }catch(InterruptedException e){
                  e.printStackTrace();
              }
              System.out.println(Thread.currentThread().getName() + "主线程运行结束!");
         }
    }
  • 相关阅读:
    题解 P3960 【列队】
    题解 P3825 【[NOI2017]游戏】
    题解 P3385 【【模板】负环】
    luoguP1555 尴尬的数字(暴力+map)
    luogu P1775 古代人的难题_NOI导刊2010提高(02)(斐波纳契+数学)
    luogu P1405 苦恼的小明(欧拉定理)
    luogu P2117 小Z的矩阵(结论题)
    BZOJ2870 最长道路tree(并查集+LCA)
    BZOJ 4668 冷战(按秩合并并查集+LCA)
    BZOJ 3376 [Usaco2004 Open]Cube Stacking 方块游戏(带权并查集)
  • 原文地址:https://www.cnblogs.com/jwentest/p/7586183.html
Copyright © 2020-2023  润新知