2

画像をダウンロードしてSDカードに保存するアプリを開発しています。SDカードと対話するクラスは次のようになります...

public class SDCardImageManagerImp implements SDCardImageManager {

    private Context context;

    @Inject
    public SDCardImageManagerImp(Context context) {
        this.context = context;
    }

    @Override
    public void saveToSDCard(String id, Bitmap bitmap) throws FileNotFoundException {
        FileOutputStream outputStream = context.openFileOutput(id, Context.MODE_PRIVATE);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    }

    @Override
    public Bitmap getImage(String id) throws FileNotFoundException {
        return BitmapFactory.decodeStream(context.openFileInput(id));
    }

    @Override
    public void deleteImage(String id) {
        context.deleteFile(id);
    }

}

複数のスレッドでディスクへの書き込みを読み取るとパフォーマンスが低下する可能性があるため、このクラスはシングルトンです。ただし、データをダウンロードしてこのクラスに送信する複数のスレッドがあります。だから私の質問は、このクラスのすべてのメソッドに同期化されたキーが機能する必要があるということですか?

4

2 に答える 2

2

Why not create a Thread Pool of Image Writers using the writerThreadPool = Executors.newFixedThreadPool(numThreads). This way, you call writerThreadPool.submit() allowing the image to be queued up for writing as a thread becomes available. The Thread Pool will handle the io throttling for you, since it will only ever allow the given number of threads that you create. And by using the Executors.newFixedThreadPool, you can easily play with the number of threads by changing numThreads to allow more or less threads to get the desired performance.

于 2012-10-25T12:06:16.120 に答える
1

仮定が有効かどうかはわかりませんが(SDカードへのI / Oを調整する必要があります)、有効である場合でも、これを構成可能にする必要があります。これは、synchronizedキーワードでは許可されません。

セマフォのカウント(および許可の数を設定するための構成ファイル)のようなものはどうですか?

于 2012-10-25T11:55:36.027 に答える