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

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

    一手遮天 Android - 异步和多线程: Thread 的异常处理

    示例如下:

    /async/ThreadDemo5.java

    /**
     * 演示 Thread 的异常处理
     */
    
    package com.webabcd.androiddemo.async;
    
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    import com.webabcd.androiddemo.R;
    
    public class ThreadDemo5 extends AppCompatActivity {
    
        private TextView _textView1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_async_threaddemo5);
    
            _textView1 = (TextView) findViewById(R.id.textView1);
    
            // 在 run 内部 try catch
            sample1();
    
            // 捕获指定 thread 对象的未处理异常
            sample2();
    
            // 捕获全部 thread 对象的未处理异常
            sample3();
        }
    
        private void sample1() {
            Thread thread1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    // 在 run 内部 try catch
                    try {
                        int x = 10 / 0;
                    } catch (Exception ex) {
                        writeMessage("thread1: " + ex.toString());
                    }
                }
            });
            thread1.setDaemon(true);
            thread1.setName("thread1");
            thread1.start();
        }
    
        private void sample2() {
            Thread thread2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    int x = 10 / 0;
                }
            });
            thread2.setDaemon(true);
            thread2.setName("thread2");
            // 捕获指定 thread 对象的未处理异常
            thread2.setUncaughtExceptionHandler(new MyThreadUncaughtExceptionHandler());
            thread2.start();
    
        }
        class MyThreadUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
            public void uncaughtException(Thread t, Throwable e) {
                writeMessage("MyThreadUncaughtExceptionHandler " + t.getName() + ": " + e.toString());
            }
        }
    
        private void sample3() {
            // 捕获全部 thread 对象的未处理异常
            Thread.setDefaultUncaughtExceptionHandler(new GlobalThreadUncaughtExceptionHandler());
    
            Thread thread3 = new Thread(new Runnable() {
                @Override
                public void run() {
                    int x = 10 / 0;
                }
            });
            thread3.setDaemon(true);
            thread3.setName("thread3");
            thread3.start();
        }
        class GlobalThreadUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
            public void uncaughtException(Thread t, Throwable e) {
                writeMessage("GlobalThreadUncaughtExceptionHandler " + t.getName() + ": " + e.toString());
            }
        }
    
        private void writeMessage(final String message) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    _textView1.append(String.format("%s
    ", message));
                }
            });
        }
    }
    
    

    /layout/activity_async_threaddemo5.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" />
    
    </LinearLayout>
    

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

  • 相关阅读:
    理解BSTR数据类型 神奇的BSTR
    char *
    _variant_t和_bstr_t
    数据库中创建表(包括创建主键,外键,非空列,唯一)
    使用ADO实现BLOB数据的存取 -- ADO开发实践之二
    sql server 2005 修改动态端口,连接字符串为:需要改成:IP地址+逗号+端口号才行
    Bilateral Filtering(双边滤波) for SSAO
    关于在Arduino中调用DS1302模块
    关于电机驱动扩展板 L293D 马达板Arduino
    Arduino教程资料汇总(8月22日悄悄跟新了一下)
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_async_ThreadDemo5.html
Copyright © 2011-2022 走看看