0

アイテムのリストの長い計算 (リモート サーバーで状態を確認) を行う必要があり、アイテムの数が多くなる可能性があるため (最初は 1 から 100.000)、分割してスレッド化するのが最善であると考えました。彼ら。

これが私がしたことです:

// Retrieving list of items in the Database
Query<Platform> query = Ebean.createQuery(Platform.class, "WHERE disabled = false AND removed IS NULL");
query.order("created ASC");
Integer quantities = query.findRowCount();

int limit = (int) Math.ceil(quantities / 10.0);
PagingList<Platform> list = query.findPagingList(limit); // This will return a list of 10 pages, containing (quantities / 10) items

for (int i = 0; i < list.getPageSize(); i++) {
    CheckState task = new CheckState(list.getPage(i).getList());
    Thread worker = new Thread(task);

    // Start the thread, never call method run() direct
    worker.start();
}

そして CheckState クラス:

public class CheckState implements Runnable {
    private List<Platform> platforms;

    public CheckState(List<Platform> platforms) {
        this.platforms = platforms;
    }

    @Override
    public void run() {
        // Do the verification for each platforms
        for (Platform platform : platforms) {
            platform.executeLongComputation()
        }
    }
}

私の考えでは、スレッドの数を制限することが望ましいので、結果をそれぞれ 100 アイテムのページに分割するのではなく、内部の n アイテムのページ数を制限することを好みます。そうすることで、元のメソッドの呼び出しごとに常に最大 10 個のスレッドを使用できることがわかります (パフォーマンスを微調整するために、将来的にこれを多かれ少なかれ変更することができます)。

しかし、これが正しく機能するかどうか、それが良い実装かどうかは疑問です。たとえば、リモートサーバーが応答しないとします。内部に1000個のアイテムのリストがありますが、これにより問題が発生しませんか(外部では完了するのに時間がかかります)?

4

1 に答える 1