編集: 1)なぜ "globalCounter" は同期ですが、ない "にThread.currentThread()のgetId()。"のJava-スレッドプール - Thredsを同期
2)私はそれぞれに計算を割り当てることができます糸?どうやって?結果を扱うことはできますか?
public class Hauptprogramm {
public static final int MAX_THREADS = 10;
public static int globalCounter;
public static Integer syncObject = new Integer(0);
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(MAX_THREADS);
for (int i = 0; i < MAX_THREADS; i++) {
threadPool.submit(new Runnable() {
public void run() {
synchronized (syncObject) {
globalCounter++;
System.out.println(globalCounter);
System.out.println(Thread.currentThread().getId());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
}});
}
threadPool.shutdown();
}
}
実行されているすべての作業に対して同じものがすべて同期していると、どうして苦労しますか? – user2864740
これはどの言語が尋ねていますか? –
@ user2864740私は自分の質問を編集します。多分もっと明確になった – Cashew