943,657 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2422
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 13th, 2008
0

Re: HashMap re-set question.

I should actually qualify this part a little bit:
Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Oct 13th, 2008
0

Re: HashMap re-set question.

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.
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007
Oct 13th, 2008
0

Re: HashMap re-set question.

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Oct 13th, 2008
0

Re: HashMap re-set question.

I agree with what you said.

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

java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007
Oct 13th, 2008
0

Re: HashMap re-set question.

Read the section on work queues in the link that I posted above.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Oct 21st, 2008
0

Re: HashMap re-set question.

this is solved
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Find Code Generated
Next Thread in Java Forum Timeline: Help with JLabel





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC