HashMap re-set question.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2007
Posts: 4,484
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: HashMap re-set question.

 
0
  #11
Oct 13th, 2008
I should actually qualify this part a little bit:
Originally Posted by Ezzaral View Post
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.
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: HashMap re-set question.

 
0
  #12
Oct 13th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,484
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: HashMap re-set question.

 
0
  #13
Oct 13th, 2008
Perhaps, but again it all depends on the processes, the hardware, how you divide and synchronize the work, etc. There isn't a single global answer there.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: HashMap re-set question.

 
0
  #14
Oct 13th, 2008
I agree with what you said.

Can you take a look at the following and see if its better now?

  1. public class MigrationUtility {
  2. private HashMap map = new HashMap ();
  3.  
  4. public static void main(String[] args){
  5. MigrationUtility m = new MigrationUtility ();
  6. m.loadMap();
  7.  
  8. for (int j=0; j < 10; j++) {
  9. new Thread(new MigrationUtilityThread(m.map)).start();
  10. }
  11. }
  12.  
  13. private void loadMap () {
  14. // This will be populated properly.
  15. // For now its dummy values for test only.
  16. for (int i = 0;i < 50; i++) {
  17. map.put(new Integer(i), new String("Some Value" + i));
  18. }
  19. }
  20. }
  21.  
  22. class MigrationUtilityThread extends Thread{
  23. private HashMap m = null;
  24. MigrationUtilityThread(HashMap map){
  25. m = map;
  26. }
  27.  
  28. public void run(){
  29. for (int j=0; j < m.size(); j++) {
  30. if (!m.get(new Integer(j)).toString().equalsIgnoreCase("Processed")) {
  31. m.put(new Integer(j), new String("Processed"));
  32. processItem(this.getName());
  33. }
  34. }
  35. }
  36.  
  37. public synchronized void processItem(String name) {
  38. // process items here...
  39. System.out.println("Item processed with thread: "+ name );
  40. }
  41. }

And this is the output of the program:
  1. Item processed with thread: Thread-0
  2. Item processed with thread: Thread-0
  3. Item processed with thread: Thread-0
  4. Item processed with thread: Thread-0
  5. Item processed with thread: Thread-0
  6. Item processed with thread: Thread-0
  7. Item processed with thread: Thread-0
  8. Item processed with thread: Thread-0
  9. Item processed with thread: Thread-0
  10. Item processed with thread: Thread-0
  11. Item processed with thread: Thread-0
  12. Item processed with thread: Thread-0
  13. Item processed with thread: Thread-0
  14. Item processed with thread: Thread-0
  15. Item processed with thread: Thread-0
  16. Item processed with thread: Thread-0
  17. Item processed with thread: Thread-0
  18. Item processed with thread: Thread-0
  19. Item processed with thread: Thread-0
  20. Item processed with thread: Thread-0
  21. Item processed with thread: Thread-0
  22. Item processed with thread: Thread-0
  23. Item processed with thread: Thread-0
  24. Item processed with thread: Thread-0
  25. Item processed with thread: Thread-0
  26. Item processed with thread: Thread-0
  27. Item processed with thread: Thread-0
  28. Item processed with thread: Thread-0
  29. Item processed with thread: Thread-0
  30. Item processed with thread: Thread-0
  31. Item processed with thread: Thread-0
  32. Item processed with thread: Thread-0
  33. Item processed with thread: Thread-0
  34. Item processed with thread: Thread-0
  35. Item processed with thread: Thread-0
  36. Item processed with thread: Thread-0
  37. Item processed with thread: Thread-0
  38. Item processed with thread: Thread-0
  39. Item processed with thread: Thread-0
  40. Item processed with thread: Thread-0
  41. Item processed with thread: Thread-0
  42. Item processed with thread: Thread-0
  43. Item processed with thread: Thread-16
  44. Item processed with thread: Thread-4
  45. Item processed with thread: Thread-6
  46. Item processed with thread: Thread-8
  47. Item processed with thread: Thread-2
  48. Item processed with thread: Thread-12
  49. Item processed with thread: Thread-18
  50. Item processed with thread: Thread-10
  51. Item processed with thread: Thread-14

I appreciate your comments.

Thanks,
Last edited by new_2_java; Oct 13th, 2008 at 5:22 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,484
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: HashMap re-set question.

 
0
  #15
Oct 13th, 2008
Read the section on work queues in the link that I posted above.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: HashMap re-set question.

 
0
  #16
Oct 21st, 2008
this is solved
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC