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?
Threading is usually referred to having multiple processes working at the same time on a single CPU (well actually not you think they do but they switch very fast between them). Parallelism is having multiple processes working at the same time on multiple CPU's.
By formal definition, multithreading refers to the ability of a processor to execute multiple threads concurrently, where each thread runs a process. Whereas multiprocessing refers to the ability of a system to run multiple processors concurrently, where each processor can run one or more threads.
A multi-threaded program will take advantage of additional threads — and cores — to distribute the load of the program more efficiently, as opposed to have one poor core do all the work while the others simply watch. The premise of concurrency is to run two or more different programs, kind of at the same time.
Threads, or lightweight processes, are essentially multiple sequences of control within a single process that share portions of a common address space.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With