multithreading 多线程和并行编程的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8271405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:17:28  来源:igfitidea点击:

Difference between multi-threading and parallel programming?

multithreadingparallel-processing

提问by previous_developer

I have a quad processor. I coded something like that in java;

我有一个四核处理器。我用java编写了类似的东西;

Some.java;

一些.java;

public class Some extends Thread {
    private SharedData sharedVal;
    private String name;

    public Some(SharedData val, String threadName) {
        sharedVal = val;
        name = threadName;
    }

    public void run() {
        int temp;
        while(true) {
            temp = sharedVal.GetValue() + 1;
            sharedVal.SetValue(temp);
        }
    }
}

SharedData.java;

共享数据.java;

public class SharedData {
    private int value;

    SharedData() {
        value = 0;
    }

    public void SetValue(int d) {
        value = d;
    }

    public int GetValue() {
        return value;
    }
}

Program.java;

程序.java;

public class Program {
    public static void main(String[] args) {
        SharedData test = new SharedData();

        Some t1 = new Some(test, "thread1");
        Some t2 = new Some(test, "thread2");
        Some t3 = new Some(test, "thread3");
        Some t4 = new Some(test, "thread4");

        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}

I run program and check processor graphics, each processor part looks working around %90.

我运行程序并检查处理器图形,每个处理器部件看起来都在 %90 左右工作。

My question is; if i can use system resources like this, what is the parallel programming? Am i getting it wrong? I saw an example on c# using processor count, what is the deal with that?

我的问题是;如果我可以这样使用系统资源,什么是并行编程?我弄错了吗?我在 c# 上看到了一个使用处理器数量的例子,这是怎么回事?

回答by Tudor

Parallel programming means using a set of resources to solve some problem in less time by dividing the work. This is the abstract definition and it relies on this part: solve some problem in less time by dividing the work. What you have shown in your code is not parallel programming in the sense that you are not processing data to solve a problem, you are merely calling some methods on multiple threads. While this is "parallel", it is not globally solving a problem.

并行编程是指使用一组资源,通过分工,在更短的时间内解决一些问题。这是抽象的定义,它依赖于这一部分:通过分工在更短的时间内解决一些问题。您在代码中显示的不是并行编程,因为您不是在处理数据来解决问题,您只是在多个线程上调用某些方法。虽然这是“并行”的,但它并不是全局解决问题。

The issue with the processor load has a connection with parallel programming in the sense that parallel parallel programming aims to keep all computational elements as busy as possible. But simply keeping the CPU busy does not mean that you are doing parallel programming.

处理器负载问题与并行编程有关,因为并行并行编程旨在使所有计算元素尽可能保持忙碌。但简单地保持 CPU 忙碌并不意味着您正在执行并行编程。

Lastly, parallel programming extends well beyond multithreading and can take place among processes running on the same machine or on different machines.

最后,并行编程远远超出了多线程,可以在同一台机器或不同机器上运行的进程之间进行。

回答by user

Parallel programming is the whole concept and multi-threading is one of the specific way to do parallel programming. For example, you can also do parallel programming by MapReduce where each task can run on separate process on different systems. On the other hand, multi-threaded program does not necessarily mean the program is parallel. It is possible to run multi-threaded program on single core machine, in which case the program is not being executed parallely.

并行编程是整个概念,多线程是进行并行编程的具体方式之一。例如,您还可以通过 MapReduce 进行并行编程,其中每个任务可以在不同系统上的单独进程上运行。另一方面,多线程程序并不一定意味着程序是并行的。可以在单核机器上运行多线程程序,在这种情况下,程序不是并行执行的。

回答by helloworld922

Parallel programming is a super set of multi-threading (i.e. multi-threading is a way to parallel program, but there are other ways to write parallel programs, for example multi-process programs).

并行编程是多线程的超集(即多线程是并行程序的一种方式,但还有其他方式可以编写并行程序,例如多进程程序)。

The main difference between threads and process:

线程和进程的主要区别:

threads in the same process can share memory resources.

同一进程中的线程可以共享内存资源。

Processes must explicitly communicate any information they wish to share to other processes.

进程必须明确传达他们希望与其他进程共享的任何信息。