zoukankan      html  css  js  c++  java
  • 一手遮天 Android

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - 异步和多线程: Thread 演示 join() 的用法

    示例如下:

    /async/ThreadDemo3.java

    /**
     * 演示 Thread 的 join 的用法
     *
     * t.join() 方法会阻塞调用此方法的线程,直到线程 t 完成后(或者达到了 join() 指定的超时时间后),此线程再继续
     */
    
    package com.webabcd.androiddemo.async;
    
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    
    import com.webabcd.androiddemo.R;
    
    public class ThreadDemo3 extends AppCompatActivity {
    
        private final String LOG_TAG = "ThreadDemo3";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_async_threaddemo3);
    
            sample1();
            sample2();
        }
    
        // 执行结果如下:
        // thread1 执行完了
        // sample1 执行完了
        private void sample1() {
            Thread thread1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    Log.d(LOG_TAG, "thread1 执行完了");
                }
            });
            thread1.setDaemon(true);
            thread1.setName("thread1");
            thread1.start();
    
            try {
                thread1.join(); // 这里 sample1 线程会阻塞,等线程 thread1 执行完后再继续
            } catch (InterruptedException e) {
    
            }
    
            Log.d(LOG_TAG, "sample1 执行完了");
        }
    
        // 执行结果如下:
        // sample2 执行完了
        // thread2 执行完了
        private void sample2() {
            Thread thread2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
    
                    }
                    Log.d(LOG_TAG, "thread2 执行完了");
                }
            });
            thread2.setDaemon(true);
            thread2.setName("thread2");
            thread2.start();
    
            try {
                thread2.join(1000); // 这里 sample2 线程会阻塞,等待 1 秒后再继续(此时线程 thread2 还没有执行完)
            } catch (InterruptedException e) {
    
            }
    
            Log.d(LOG_TAG, "sample2 执行完了");
        }
    }
    
    

    /layout/activity_async_threaddemo3.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="详见 java 代码" />
    
    </LinearLayout>
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    68.css 3d 卡片翻转效果
    67.canvas绘制常规图形
    66.环形加载动画(canvas/svg)
    65.canvas画一个表(2)
    64.canvas画一个表(1)
    63.实现一个拖拽排序
    62.textarea 自适应高度
    co co a P o a d s的使用
    在MJRefresh的基础上实现动画的自定义和自动下拉刷新
    iOS26 AFNetworking
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_async_ThreadDemo3.html
Copyright © 2011-2022 走看看