15

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

Some.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;

public class SharedData {
    private int value;

    SharedData() {
        value = 0;
    }

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

    public int GetValue() {
        return value;
    }
}

Program.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.

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?

Blue
  • 22,608
  • 7
  • 62
  • 92
previous_developer
  • 10,579
  • 6
  • 41
  • 66
  • 1
    Exact duplicate:http://stackoverflow.com/questions/2287695/is-parallel-programming-multithread-programming – Cratylus Nov 25 '11 at 16:03
  • 1
    Mmmm, it may be worthwhile to keep it open, since he is asking about his particular program and not in general... I dunno. – Tudor Nov 25 '11 at 16:09

3 Answers3

25

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.

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

Tudor
  • 61,523
  • 12
  • 102
  • 142
  • So using multithreading approach is a part of Parallell Programming, but Parallel Programming is not just using multithreading? – Burak Karakuş Jun 05 '20 at 14:09
  • @Burak Karakuş: Yes, multithreading is one way of achieving parallelism, but not the only one. At the same time, not every use of multithreading is parallelism. – Tudor Jun 05 '20 at 15:00
13

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.

user
  • 5,335
  • 7
  • 47
  • 63
6

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.

helloworld922
  • 10,801
  • 5
  • 48
  • 85