| | |
HashMap re-set question.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
I should actually qualify this part a little bit: If the process is CPU-bound, such as performing a lot of computations or loops, adding additional threads won't offer any improvement and may even make it slower due to the additional overhead of managing the threads themselves (synchronization, context switching, etc.). If the process performs some tasks that are constrained waiting on other resources like IO, multiple threads may offer improved performance even on single processor machines.
This article on thread pools and work queues covers some of these issues:
http://www.ibm.com/developerworks/li...j-jtp0730.html
•
•
•
•
Additionally, if you are running on a single processor, starting 10 threads won't make it go faster - it will just have to time-slice the processing of each thread anyway.
This article on thread pools and work queues covers some of these issues:
http://www.ibm.com/developerworks/li...j-jtp0730.html
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
Ezzaral,
The program is actually IO intensive. I am trying to write a migration program, which will read each content/metadata from a source repository and create its corresponding object (contetnt/metadata) to a target repository.
So, the program will be IO intensive as apposed to CPU, I believe.
So, will multi-thread help me in this senario?
Thanks for the suggestions.
The program is actually IO intensive. I am trying to write a migration program, which will read each content/metadata from a source repository and create its corresponding object (contetnt/metadata) to a target repository.
So, the program will be IO intensive as apposed to CPU, I believe.
So, will multi-thread help me in this senario?
Thanks for the suggestions.
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
I agree with what you said.
Can you take a look at the following and see if its better now?
And this is the output of the program:
I appreciate your comments.
Thanks,
Can you take a look at the following and see if its better now?
java Syntax (Toggle Plain Text)
public class MigrationUtility { private HashMap map = new HashMap (); public static void main(String[] args){ MigrationUtility m = new MigrationUtility (); m.loadMap(); for (int j=0; j < 10; j++) { new Thread(new MigrationUtilityThread(m.map)).start(); } } private void loadMap () { // This will be populated properly. // For now its dummy values for test only. for (int i = 0;i < 50; i++) { map.put(new Integer(i), new String("Some Value" + i)); } } } class MigrationUtilityThread extends Thread{ private HashMap m = null; MigrationUtilityThread(HashMap map){ m = map; } public void run(){ for (int j=0; j < m.size(); j++) { if (!m.get(new Integer(j)).toString().equalsIgnoreCase("Processed")) { m.put(new Integer(j), new String("Processed")); processItem(this.getName()); } } } public synchronized void processItem(String name) { // process items here... System.out.println("Item processed with thread: "+ name ); } }
And this is the output of the program:
Java Syntax (Toggle Plain Text)
Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-0 Item processed with thread: Thread-16 Item processed with thread: Thread-4 Item processed with thread: Thread-6 Item processed with thread: Thread-8 Item processed with thread: Thread-2 Item processed with thread: Thread-12 Item processed with thread: Thread-18 Item processed with thread: Thread-10 Item processed with thread: Thread-14
I appreciate your comments.
Thanks,
Last edited by new_2_java; Oct 13th, 2008 at 5:22 pm.
![]() |
Similar Threads
- Question about string pattern matching (Java)
- Database manipulation with AVL tree.. please help (Java)
Other Threads in the Java Forum
- Previous Thread: Find Code Generated
- Next Thread: Help with JLabel
Views: 1577 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation binary bluetooth c# capture chat chatprogramusingobjects class classes client code color component count database design draw eclipse eclipsedevelopment encryption error event exception file fractal game givemetehcodez graphics gridlayout gui helpwithhomework high html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jmf jni jpanel julia keyword linux list loop macintosh map method methods mobile netbeans newbie number object oracle os print problem producer program programming project projectideas read recursion replaysolutions scanner screen server set size sms socket sort sql string swing test threads time transfer transforms tree ui unicode windows






