多线程中的一些关键字
Runnable
Thread
ThreadPoolExecutor
ScheduleExecutor
关于线程同步的关键字
Synchronized
Lock
Semaphore(信号量):http://www.cnblogs.com/liuling/p/2013-8-20-03.html
--------------------------------------------------------------------分割线--------------------------------------------------------------------------------
为什么使用多线程
IO操作(文件操作,网络操作,数据库操作...)
复杂操作
定时运算
综上,耗时的操作(一般超过50ms的操作)都建议用多线程处理,避免主线程(UI线程)阻塞。
如何使用多线程或异步操作(不一定是多线程)
Thread
Handler
AsyncTask
AsyncTaskLoader (Android3.0之后) --- 有个子类 CursorLoader 可以方便的用于操作数据库与UI的交互。
--------------------------------------------------------------------分割线--------------------------------------------------------------------------------
//代码
ImageView mImageView = null; private final String IMAGE_URL = "http://b.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=02b2417e8744ebf8797c6c6db890bc4f/fc1f4134970a304e4ccdd887d1c8a786c8175cdb.jpg"; // 异步任务类AsyncTask,实现网络图片的下载,显示与ImageView private class DownloadImageTask extends AsyncTask<String, Void, Drawable> { @Override protected Drawable doInBackground(String... arg0) { // TODO Auto-generated method stub return getDrawableFromNet(arg0[0]); } @Override protected void onPostExecute(Drawable result) { // TODO Auto-generated method stub mImageView.setImageDrawable(result); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageView = (ImageView) findViewById(R.id.imageContent); // 所有的操作都放在UI线程里面,造成线程的阻塞,较新版本的Android会抛出NetWorkOnMainThreadException的异常 findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Drawable drawable = getDrawableFromNet(IMAGE_URL); mImageView.setImageDrawable(drawable); } }); // 在一个新的线程中处理UI线程的ImageView,会抛出异常。线程不安全 findViewById(R.id.button2).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub Drawable drawable = getDrawableFromNet(IMAGE_URL); mImageView.setImageDrawable(drawable); } }).start(); } }); // 通过mImageView的post(Runnable)方法来执行异步操作,可以实现网络图片的异步加载 findViewById(R.id.button3).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub final Drawable drawable = getDrawableFromNet(IMAGE_URL); mImageView.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub mImageView.setImageDrawable(drawable); } }); } }).start(); } }); // 通过AsyncTask类来实现异步操作,实现网络图片的加载 findViewById(R.id.button4).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new DownloadImageTask().execute(IMAGE_URL); } }); } // 从指定的url下载网络图片 private Drawable getDrawableFromNet(String imageUrl) { Drawable drawable = null; try { drawable = Drawable.createFromStream( new URL(imageUrl).openStream(), "image.gif"); } catch (IOException e) { // TODO Auto-generated catch block Log.d("test", e.getMessage()); e.printStackTrace(); } if (drawable == null) { Log.d("test", "image is null"); } else { Log.d("test", "image not null"); } return drawable; }
代码(demo)下载地址:
https://github.com/lzj1991/ThreadActivity
编译环境eclipse,如果遇到如下错误:
error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.
原因是無法找到原來的android-support-v7的支持包,需要重新添加。
http://www.hksilicon.com/kb/articles/603216/AndroidError-retrieving-parent-for-item-No-resource-found-that-matches-the-given-name-ThemeAppCompatLight
<p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>