| | |
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
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows






