Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 966
  • Last Modified:

Mono Threading

What's the difference in terms of functions and coding between using a mono and multi threaded application if i want to calculate this:   y = 3x3 + 5x2 -10x + 10 (just an example)

Thank you.
0
VanJava
Asked:
VanJava
1 Solution
 
objectsCommented:
none really. it would evaluated in a single thread.
0
 
nullsquidCommented:
I believe threading is useful if your calculation needs to pause for another process at different stages. for example, if the sum was

y=(a very complicated calculation which requires disk access)+(another very complicated calculation which requires disk access)

you could thread each of the (very complicated calculation which requires disk access) such that as one of the processes was waiting for the disk access before it could proceed, the other one could be using the spare flops in the cpu. But for really simple things, no improvement would be seen with multithreading, indeed, it may even be slower.
0
 
VanJavaAuthor Commented:
Can you please show me the codes using mono and multi threaded application to achive above calculation so i can run it and see  my self the effectiveness of both. Thanks
0
 
pellepCommented:
Since your example is so trivial, you would get worse performance multi-threading it than doing it in one go in a single thread since the overhead-cost of setting up the threads would far out-weigh the actual calculation. Anyway, in semi-pseudo code


public class Worker1 implements Runnable {
public long result;
public void run() {
result = 3*3;
}
}
public class Worker2 implements Runnable {
public long result;
public void run() {
result = 5*2;
}
}
public class Worker3 implements Runnable {
public long result;
public void run() {
result = 10*4;
}
}
 
.....
public static void main(String[] args) {
Worker1 worker1 = new Worker1();
Thread t1 = new Thread(worker1);
t1.start();
Worker2 worker2 = new Worker2();
Thread t2 = new Thread(worker2);
t2.start();
Worker3 worker3 = new Worker3();
Thread t3 = new Thread(worker3);
t3.start();
// wait for threads to complete
t1.join();
t2.join();
t3.join();
System.out.println("" + (worker1.result + worker2.result + worker3.result));
 
}

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now