Solved

Mono Threading

Posted on 2009-05-06
4
931 Views
Last Modified: 2013-11-23
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
Comment
Question by:VanJava
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24313707
none really. it would evaluated in a single thread.
0
 
LVL 3

Expert Comment

by:nullsquid
ID: 24313836
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
 

Author Comment

by:VanJava
ID: 24313893
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
 
LVL 4

Accepted Solution

by:
pellep earned 500 total points
ID: 24315177
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error trying to install RTMT Win7 5 41
xampp tool 12 30
tomcat administrtor 12 34
Need help making a program or form for inventory that exports to txt 8 13
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now