zoukankan      html  css  js  c++  java
  • 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() + "主线程运行结束!");
         }
    }
  • 相关阅读:
    ASP.NET 生命周期(原文翻译)
    JSON 小记
    Unity5-ABSystem(二):AssetBundle导出
    Unity5-ABSystem(一):AssetBundle原理
    《Unity 3D游戏客户端基础框架》概述
    Unity中的资源管理
    Unity中的优化技术
    深入浅出聊Unity3D项目优化:从Draw Calls到GC
    Incremental builds for IL2CPP
    Unity3D图像后处理特效——Depth of Field 3.4
  • 原文地址:https://www.cnblogs.com/jwentest/p/7586183.html
Copyright © 2011-2022 走看看