यहां एक मुख्य है जो एक साधारण गिनती लूप को तीन तरीकों से चलाता है:

  1. एकल पिरोया

  2. 2 थ्रेड इनलाइन कोड का उपयोग करते हैं जो दो अलग थ्रेड ऑब्जेक्ट बनाता है

  3. काउंटिंगथ्रेड वर्ग के उदाहरणों का उपयोग करते हुए 2 धागे जो थ्रेड से विरासत में मिलते हैं

package main;

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        demo();
    }
    public static void demo() {

        final long limit = 100_000_000_000L;
        long startTime = System.currenatTimeMillis();
        for (long i = 0; i < limit; i++) {
            // Nothing to see here, just counting
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Single threaded: Total execution time: " + (endTime - startTime) + " milliseconds.");

        // Now try it in two threads. Each thread will perform 1/2 of the counting
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (long i = 0; i < limit/2; i++) {
                    // Nothing to see here, just counting
                }
            }
        });  
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (long i = limit/2; i < limit; i++) {
                    // Nothing to see here, just counting
                }
            }
        });  
        startTime = System.currentTimeMillis();
        t1.start();
        t2.start();
        // Join t1 until it ends, then join t2 until it ends. Note that t1 and t2 are running in parallel with this thread.
        try {t1.join();} catch (InterruptedException e) {}
        try {t2.join();} catch (InterruptedException e) {}
        endTime = System.currentTimeMillis();
        System.out.println("2 threaded using inline code: Total execution time: " + (endTime - startTime) + " milliseconds.");

        // Now try it with 2 instances of the CountingThread class.
        ArrayList<CountingThread> countingThreads = new ArrayList<CountingThread>();
        int numberOfThreads = 2;
        long increment = limit / numberOfThreads;
        for (int i = 0; i < numberOfThreads; i++) {
            long start, end;
            start = i * increment;
            end = start + increment;
            countingThreads.add(new CountingThread(start, end));
        }
        // Launch all the threads to run in parallel
        startTime = System.currentTimeMillis();
        for (int i = 0; i < numberOfThreads; i++) {
            countingThreads.get(i).run();
        }

        // Wait for all the threads to finish
        for (int i = 0; i < numberOfThreads; i++) {
            try {countingThreads.get(i).join();} catch(InterruptedException ex) {}
        }
        endTime = System.currentTimeMillis();
        System.out.println(numberOfThreads + " threaded using classes: Total execution time: " + (endTime - startTime) + " milliseconds.");
    }   
}

यहां वह वर्ग है जो थ्रेड से प्राप्त होता है:

package main;

/**
 * Count from one long int up to another long int. Really simple
 *
 */
public class CountingThread extends Thread {

    private long start, end;
    public CountingThread(long start, long end) {
        this.start = start;
        this.end = end;
    }

    @Override
    public void run() {
        for(long i = start; i <= end; i++) {

        }
//      System.out.println("Thread counted from " + start + " to " + end);
    }
}

यहाँ आउटपुट है:

Single threaded: Total execution time: 40379 milliseconds.
2 threaded using inline code: Total execution time: 23312 milliseconds.
2 threaded using classes: Total execution time: 40358 milliseconds.

ऐसा लगता है कि विधियों 2 और 3 में लगभग समान समय लगना चाहिए। उसके साथ क्या है?

मशीन में 4 कोर हैं।

0
nicomp 23 जून 2019, 15:36

1 उत्तर

सबसे बढ़िया उत्तर

आपने गलती की है और #स्टार्ट के बजाय #रन को कॉल करें। रन विधि को उसी थ्रेड में निष्पादित किया जाता है।

countingThreads.get(i).run();
6
AdamSkywalker 23 जून 2019, 15:39