Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的多个线程是否在不同的CPU上执行?_Java_Python_Multithreading_Parallel Processing_Thread Safety - Fatal编程技术网

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

Java中的多个线程是否在不同的CPU上执行?,java,python,multithreading,parallel-processing,thread-safety,Java,Python,Multithreading,Parallel Processing,Thread Safety,在Python中,线程用于任务执行需要等待的情况。简单地说,Python线程不会在不同的CPU上执行 例如,考虑CPU绑定函数;以下程序中的count() 性能测试\u顺序.py import time def count(n): while(n > 0): n = n - 1; def main(): start_time = time.time(); count(100000000); count(100000000); en

在Python中,线程用于任务执行需要等待的情况。简单地说,Python线程不会在不同的CPU上执行

例如,考虑CPU绑定函数;以下程序中的count()

性能测试\u顺序.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();`
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();
性能测试螺纹.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();`
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倍)

这是由于,这会阻止多个本机线程同时执行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);

    }  
}`
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);

    }  
}
PerformanceTestThreaded.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);

    }  
}`
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中的线程是否提供并行编程,也就是说,这两个不同的线程是否与Python不同,在系统的不同可用处理器上运行? 我想了解更多关于Java中线程如何工作的信息,请提供一些参考资料。


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

它们可能是。他们可能不是。取决于操作系统决定做什么。我在过去的某个地方读到,如果可以的话,java确实使用多核,但是我现在找不到源代码。请看“我想了解更多关于Java中线程如何工作的信息,请提供一些参考资料。”这个问题对于堆栈溢出来说不是主题,因为它是对非现场资源的请求。如果指定了它,我会感到惊讶,它看起来像是一个VM实现细节。线程用于“需要等待的任务”这一事实与它们是否在不同的CPU上调度没有关系。您可以将多线程Python程序直接移植到Java,而无需更改其逻辑,线程可能会在不同的CPU上运行。。。