android 多线程异步下载文件,Android异步任务处理_多线程
主进程更新UI: 会阻塞MainActivity.java Java文件package com.example.hongweipc.first;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;public class MainActivity exte
主进程更新UI: 会阻塞
MainActivity.java Java文件
package com.example.hongweipc.first;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Thread.sleep(1000);
System.out.println(">>>>>>>>>>>>>>>>>>>Tick");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
activity_main.xml 布局文件
AndroidManifest.xml
运行情况
与上面代码只有MainActivity.java文件有修改
线程更新UI:不会阻塞
package com.example.hongweipc.first;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
try {
while (true) {
sleep(1000);
System.out.println(">>>>>>>>>>>>>>>>>>>Tick");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
});
}
}
线程运行情况
更多推荐
所有评论(0)