多线程使用方式

第一种:使用匿名类实现Runnable接口的方式(推荐)

new Thread(new Runnable() { // 匿名类的Runnable接口
            @Override
            public void run() {
                Test();
            }
        }).start();

第二种:新建一个类继承Thread

public class SubThread_HH extends Thread{
    private final String TAG = this.getClass().getSimpleName();
 
    @Override
    public void run()
    {
       // while(true)
        {
            String str = "55";
            byte nData = Integer.valueOf(str, 16).byteValue();
            int nTmp = Integer.valueOf(str, 10).intValue();
            Log.d(TAG, "run: " + nTmp);
        }
    }
}

(2)在MainActivity中调用

new SubThread_HH().start();

第三种:实现Runnable接口

(1)新建一个Java Class 文件

public class SubThread_TT implements Runnable{
    private final String TAG = getClass().getSimpleName();
 
    @Override
    public void run() {
        String str = "98";
        byte u8Data = Integer.valueOf(str, 10).byteValue();
        Log.d(TAG, "run: " + u8Data);
    }
}

(2)在MainActivity中调用

new Thread(new SubThread_TT()).start();

Supongo que te gusta

Origin blog.csdn.net/weixin_38107457/article/details/121621887
Recomendado
Clasificación