Android
  • Introduction
  • Android Studio
    • AS的项目结构
    • adb
    • aapt
    • dx
    • Gradle
    • Kotlin on android
  • Smaller
  • decompiler
  • MISC
  • 框架 framework
  • 设计模式
  • dex
  • reinforce加固
  • code
    • Java Tips
      • 负数 negative
      • The Java Tutorials
        • 原始数据类型 Primitive Data Types
      • 运算符
        • 一元运算符
        • 算术运算符
        • 移位运算符
        • 关系运算符
      • 逻辑运算符
        • 逻辑 非 ! 关系值表
        • 逻辑 与 && 关系值表
        • 逻辑 或 || 关系值表
        • 与 & And
        • 或 | Or
        • 非 ~ Nor
        • 异或 ^ Xor
        • 赋值运算符
        • tips
      • == equals
      • Try Catch finally
        • 有意思的东西
      • String、StringBuilder、StringBuffer区别
      • inner classes、nested static classes
    • runtime_memory
    • javaStackTrace
    • Guava
    • FFMPEG
    • GoogleSamples
    • Full Kotlin Reference
    • release屏蔽Log代码
    • Thread
  • ANR
  • 注解改进代码检查
Powered by GitBook
On this page
  • sleep
  • join
  • yield

Was this helpful?

  1. code

Thread

sleep

/**
 * Created by gaopj on 2017/8/4.
 */
public class TestThreadSleep implements Runnable {
    int num = 0;
    @Override
    public void run() {
        for (int i=0;i<100;i++) {
            num++;
        }
    }

    public static void main(String[] args) {
        TestThreadSleep ttj = new TestThreadSleep();
        Thread t = new Thread(ttj);
        t.start();
        try {
            t.sleep(100);//
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(ttj.num);
    }
}

join

/**
 * Created by gaopj on 2017/8/4.
 */
public class TestThreadJoin implements Runnable {
    int num = 0;
    @Override
    public void run() {
        for (int i=0;i<100;i++) {
            num++;
        }
    }

    public static void main(String[] args) {
        TestThreadJoin ttj = new TestThreadJoin();
        Thread t = new Thread(ttj);
        t.start();
        try {
            t.join();//线程加入, t 执行完才向后继续执行
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(ttj.num);
    }
}

yield

/**
 * Created by gaopj on 2017/8/4.
 */
public class TestThreadYield implements Runnable {
    int num = 0;
    @Override
    public void run() {
        for (int i=0;i<100;i++) {
            num++;
        }
    }

    public static void main(String[] args) {
        TestThreadYield ttj = new TestThreadYield();
        Thread t = new Thread(ttj);
        t.start();
        while (Thread.activeCount()>1) {
            Thread.yield();
        }
        System.out.println(ttj.num);
    }
}

sleep join yield 预期结果都能正常显示,但sleep是最不合理的.

Previousrelease屏蔽Log代码NextANR

Last updated 5 years ago

Was this helpful?