User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,094 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,427 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 289 | Replies: 1
Reply
Join Date: Jun 2008
Posts: 8
Reputation: amjad277 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
amjad277 amjad277 is offline Offline
Newbie Poster

Too many threads invoking synchronized method

  #1  
Jul 2nd, 2008
When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block.

What is happen when too many threads (for the same object) for example 100 Threads invoke this method and want to send data?
In which order can the Threads be executed?
Is there any queueing or buffering here?
If there is buffering, how long does it take to release the buffering data?
  1. private Lock sendMessageLock = new ReentrantLock();
  2.  
  3. public Response send(SMTInitiated data) {
  4. [b]synchronized [/b](sendMessageLock) {
  5. return send(data);
  6. }
  7. }
Last edited by Tekmaven : Jul 14th, 2008 at 10:07 pm. Reason: Added code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Posts: 4
Reputation: yzg1236 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
yzg1236 yzg1236 is offline Offline
Newbie Poster

Re: Too many threads invoking synchronized method

  #2  
Jul 14th, 2008
1. All threads start to run
2. Some thread get LOCK on some exclusive object
3. Other threads which are requiring this LOCKED object will get into SLEEP
4. After thread in step 2 release LOCKED object, thread manager will NOTIFY all other threads which are SLEEPing and waiting for this LOCKED object.
5. These threads resume running, back to step 1.

So normally system can't not make sure which one is the next resuming thread.

OS have some rules to schedule tasks. But I think you could experiment to get some clue:
  1. private Lock sendMessageLock = new ReentrantLock();
  2.  
  3. public Response send(SMTInitiated data, String threadM) {
  4. synchronized (sendMessageLock) {
  5.  
  6. System.out.println(threadM);
  7. Thread.sleep(100) ;
  8.  
  9. return send(data);
  10. }
  11. }
  12.  
  13. public void main () {
  14. Thread t1 = new Thread(new Runnable(){
  15. public void run() {
  16. for (int i=0; i < 100000;i++)
  17. send(data, "t1");
  18. }
  19. });
  20. Thread t2 = new Thread(new Runnable(){
  21. public void run() {
  22. for (int i=0; i < 100000;i++)
  23. send(data, "t2");
  24. }
  25. });
  26. Thread t3 = new Thread(new Runnable(){
  27. public void run() {
  28. for (int i=0; i < 100000;i++)
  29. send(data, "t3");
  30. }
  31. });
  32. Thread t4 = new Thread(new Runnable(){
  33. public void run() {
  34. for (int i=0; i < 100000;i++)
  35. send(data, "t4");
  36. }
  37. });
  38.  
  39. t1.start();
  40. t2.start();
  41. t3.start();
  42. t4.start();
  43.  
  44.  
  45. }
Last edited by Tekmaven : Jul 14th, 2008 at 10:05 pm. Reason: Added code tags
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Java Forum

All times are GMT -4. The time now is 6:01 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC