Created
May 24, 2011 15:38
-
-
Save zaki50/988940 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.zakky.queue; | |
import android.app.Activity; | |
import android.graphics.Bitmap; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import java.util.List; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
public class MainActivity extends Activity { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
} | |
private ExecutorService mExecutor; | |
private final Handler mHandler = new Handler(); | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
List<Uri> uriList = null; // 実際はUri を保持する List | |
final int concurrency = 5; | |
mExecutor = Executors | |
.newFixedThreadPool(Math.min(concurrency, uriList.size())); | |
for (final Uri uri : uriList) { | |
mExecutor.submit(new Runnable() { | |
@Override | |
public void run() { | |
final Bitmap bmp = get(uri); | |
mHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
// ここは UI スレッドなので bmp をセットするなどする。 | |
bmp.toString(); | |
} | |
}); | |
} | |
private Bitmap get(Uri uri) { | |
// 実際は uri から Bitmap を取得する | |
return null; | |
} | |
}); | |
} | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
if (mExecutor != null) { | |
mExecutor.shutdownNow(); | |
mExecutor = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment