2

私のスレッドはそれぞれ 2000 ミリ秒スリープし、そのようなスレッドが 10 個あるため、合計スリープ時間は少なくとも 20 秒になると予想していましたが、16 ~ 18 秒の間にしか発生しません。すでに質問されていることを質問している場合は申し訳ありません。これが私がこれまでに持っているものです:

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MyThreadPoolApp {
    public static void main(String[] args) {
        long execTime = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(1);
        ExecutorService executor = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 10; i++) {
            executor.submit(new Task());
        }
        System.out.println("threads submitted and waiting execution");

        executor.shutdown();        
        try {
            executor.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
        }
        execTime = System.currentTimeMillis() - execTime;
        System.out.format("%d threads finished execution \n",Task.getCount());
        System.out.println("thread time : " + Task.getTime());
        System.out.println("main time : " + execTime);

    }
}

タスクは次のとおりです。

public class Task implements Runnable {

    private static long totalTime; 
    private static int count; 
    public static long getTime(){ return totalTime; }
    public static int getCount(){ return count; }

    public void run() {
        count++;
        long startTime = System.currentTimeMillis();
        try {
            Thread.sleep(2000);
            totalTime += System.currentTimeMillis() - startTime;
        } catch (InterruptedException e) {}
    }
}

私の出力:

threads submitted and waiting execution  
10 threads finished execution   
thread time : 18001  
main time : 2020  
4

1 に答える 1

1

さまざまなスレッドによる totalTime の同時更新をいじっているためです。

これを試して:

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MyThreadPoolApp {
    public static void main(String[] args) {
        long execTime = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(1);
        ExecutorService executor = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 10; i++) {
            executor.submit(new Task());
        }
        System.out.println("threads submitted and waiting execution");

        executor.shutdown();
        try {
            executor.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
        }
        execTime = System.currentTimeMillis() - execTime;
        System.out.format("%d threads finished execution \n",Task.getCount());
        System.out.println("thread time : " + Task.getTime());
        System.out.println("main time : " + execTime);
    }
}

class Task implements Runnable {

    private static long totalTime;
    private static int count;
    public static long getTime(){
        synchronized(Task.class){
            return totalTime;
        }
    }
    private static void addTime(long time){
        synchronized(Task.class){
            totalTime = totalTime + time;
        }
    }
    public static int getCount(){ return count; }

    public void run() {
        count++;
        long startTime = System.currentTimeMillis();
        try {
            Thread.sleep(2000);
            addTime(System.currentTimeMillis() - startTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

編集 必要に応じて、run() メソッド内で同期します。

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MyThreadPoolApp {
    public static void main(String[] args) {
        long execTime = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(1);
        ExecutorService executor = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 10; i++) {
            executor.submit(new Task());
        }
        System.out.println("threads submitted and waiting execution");

        executor.shutdown();
        try {
            executor.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
        }
        execTime = System.currentTimeMillis() - execTime;
        System.out.format("%d threads finished execution \n",Task.getCount());
        System.out.println("thread time : " + Task.getTime());
        System.out.println("main time : " + execTime);
    }
}

class Task implements Runnable {

    private static long totalTime;
    private static int count;
    public static long getTime(){
        synchronized(Task.class){
            return totalTime;
        }
    }
    public static int getCount(){ return count; }

    public void run() {
        count++;
        long startTime = System.currentTimeMillis();
        try {
            Thread.sleep(2000);
            synchronized(Task.class){
                totalTime += System.currentTimeMillis() - startTime;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
于 2013-10-15T07:01:45.767 に答える