File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes synchronization without wait and notify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronization without wait and notify" Watch "synchronization without wait and notify" New topic
Author

synchronization without wait and notify

Abdul Aleem
Greenhorn

Joined: Jul 19, 2002
Posts: 19
Hi All,

I am not sure if this is a naive question, I would greatly appreciate
if someone could explain with a code example to solve the question below

There are two methods one synchronized and other non-synchronized e.g.



Suppose there are two Threads T1 and T2 accessing the above methods,
The question is which ever thread T1 or T2 gets to m1() then the thread
getting to m2() should wait for the thread in the synchronized method m1() to complete and then start processing m2()

Could this be achieved without creating additional locks or without using wait and notify ?

Many thanks,
Abdul
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Besides just making m2 synchronized? That's basically what you're describing.


[Jess in Action][AskingGoodQuestions]
Abdul Aleem
Greenhorn

Joined: Jul 19, 2002
Posts: 19
Hi Ernest,

Yea, not synchronizing the method m2() but use synchronized block
within m2() to solve the problem.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Why, is this a puzzle or test question?

But in any case, synchronized(this) { ... } around the whole body of m2 is the same as making the method synchronized.
Abdul Aleem
Greenhorn

Joined: Jul 19, 2002
Posts: 19
Hi Ernest,

Yea this is actually an interview question, i am not sure what did the interviewer was expecting as the answer, I am pretty much aware that using synchronized block within m2() is same as synchronizing the method itself.

The question posted was how would you achieve the coordination of two methods by using java synchronized block only and not to use wait & notify so that any thread entering m2() will wait for thread in m1()


Thanks a lot,
Abdul
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronization without wait and notify