Java中的多个线程是否在不同的CPU上执行?

时间:2017-06-25 09:54:24

标签: java python multithreading parallel-processing thread-safety

在Python中,线程用于任务执行涉及一些等待的情况。简而言之,Python线程不会在不同的CPU上执行。

例如,考虑CPU限制功能;在以下程序中count():

performance_test_sequential.py

import time 
def count(n):
    while(n > 0):
        n = n - 1;

def main():
    start_time = time.time();
    count(100000000);
    count(100000000);
    end_time = time.time();

    print("\n\n--- Total Execution Time: %s seconds ---" % (end_time - start_time));

if(__name__ == '__main__'):
main();`

performance_test_threaded.py

import threading
import time

class myThread(threading.Thread):
    def __init__(self, n):
        threading.Thread.__init__(self);
        self.n = n;

    def run(self):
        while(self.n > 0):
            self.n = self.n - 1;


def main():
    start_time = time.time();
    t1 = myThread(100000000);
    t2 = myThread(100000000);
    t1.start();
    t2.start();
    t1.join();
    t2.join();
    end_time = time.time();

    print("\nTotal Execution Time: %s seconds" % (end_time-start_time));

if(__name__ == '__main__'):
    main();

这些计划的执行时间为:

顺序:18.33秒

螺纹:45.56秒(慢2.5倍)

这是由于global interpreter lock,它阻止多个本机线程一次执行Python字节码。 简单地说,以这种方式进行并行编程在Python中是不可能的。

我在Java中创建了相同的程序,在这种情况下,与Python不同,线程程序的执行时间几乎是一半:

PerformanceTestSequential.java

import java.util.*;
public class PerformanceTestSequential
    {
        int N;
        public PerformanceTestSequential(int n)
        {
            this.N = n;
        }

        public void run(){ 
            while(this.N > 0)
            {
                (this.N)--; 
             }
        }


// 
public static void main(String args[]){ 


PerformanceTestSequential thread1 = new PerformanceTestSequential(1000000000);
PerformanceTestSequential thread2 = new PerformanceTestSequential(1000000000);


long startTime = System.nanoTime();
thread1.run();
thread2.run();
long endTime   = System.nanoTime();
long totalTime = endTime - startTime;

System.out.println("\n\nExecution Time: " + (double)totalTime / 1000000000);

    }  
}`

PerformanceTestThreaded.java

import java.util.*;
public class PerformanceTestThreaded extends Thread
{
    int N;
    public PerformanceTestThreaded(int n)
    {
        this.N = n;
    }

    public void run(){ 
    while(this.N > 0)
    {
        //System.out.print(n);
        (this.N)--;

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


    PerformanceTestThreaded thread1 = new PerformanceTestThreaded(1000000000);
    PerformanceTestThreaded thread2 = new PerformanceTestThreaded(1000000000);
    long startTime = System.nanoTime();
    thread1.start();
    thread2.start();
    try {
        //System.out.println("Waiting for thread 1 to finish.");
            thread1.join();
        } catch (InterruptedException e) {
            System.out.println("Main thread Interrupted");
        }
    try {
        //System.out.println("Waiting for thread 2 to finish.");
        thread2.join();
        } catch (InterruptedException e) {
            System.out.println("Main thread Interrupted");
        }
        long endTime   = System.nanoTime();
        long totalTime = endTime - startTime;
        System.out.println("\n\nExecution Time: " + (double)totalTime / 1000000000);

    }  
}

执行时间:

PerformanceTestSequential.java = 1.816秒

PerformanceTestThreaded.java = 0.8697秒

与Python不同,这里的线程程序更快。

我的问题是Java中的Threads是否提供并行编程,即两个不同的线程是否运行在系统的不同可用处理器上,与Python不同? 我想了解更多关于线程如何在Java中工作的内容,请提供一些资源。

请提供文档的链接,以解释不同线程如何在Java中的不同处理器上运行。

0 个答案:

没有答案