aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronize method call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronize method call" Watch "synchronize method call" New topic
Author

synchronize method call

Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

I would like to learn , If I call a non-synchronized method from a synchronized method while a thread is running, can another thread execute the non-synchronize method or It should wait the syncronized method until it finish its work with nonSynchMethod?

public synchronized void doIt()
{
nonSynchMethod();
}
public void nonSynchMehtod();
{

}
[ October 15, 2008: Message edited by: Anut Walidera ]

blog: http://tunatore.wordpress.com
SCJP 6.0 + SCWCD 1.5
Don Solomon
Ranch Hand

Joined: Jul 20, 2008
Posts: 48
yes, another thread can call the non-synchronized method


Software development is an exercise in thinking not coding.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
public class TestThr
{
public static void main(String[] a)
{
MyTH mt=new MyTH();
Thread t1=new Thread(mt);
Thread t2=new Thread(mt);
t1.setName("1st thread");
t2.setName("2nd thread");
t1.start();
t2.start();
}
}
class MyTH implements Runnable
{
public void run()
{
mt1();
}
//synchronized method
synchronized private void mt1()
{
System.out.println("synchronized method is executed

by"+Thread.currentThread().getName());
mt2();
}
//non-synchronized method
private void mt2()
{
System.out.println("non-synchronized method is executed

by"+Thread.currentThread().getName());
}
}


output is:

synchronized method is executed by 1st thread
non-synchronized method is executed by 1st thread
synchronized method is executed by 2nd thread
non-synchronized method is executed by 2nd thread


or



synchronized method is executed by 2nd thread
non-synchronized method is executed by 2nd thread
synchronized method is executed by 1st thread
non-synchronized method is executed by 1st thread


but here 2nd thread is not accessing the non-synchronized method when synchronized ,method is accessed by 1st thread.....

can you give me explanation regarding this?


SCJP5 and SCWCD1.5
Think Twice Act Wise...
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
@GaneshKumar:
but here 2nd thread is not accessing the non-synchronized method

Why not? When you call the non-synshronized code mt2() from within your synchronized code, any thread that executes mt1() would invariably land up calling mt2()..
So when the second thread starts, it first calls mt1() and then from within mt1(), it calles mt2() !


Preparing for SCJP 6.0
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
Intersting question..I dont know the answer to the question but this is what I think..
The thread that enters into a synchronized code acquires the lock and if any non-synchronized code is called from within that synchorinized code, it acquires a lock on the non-synchronized code as well. So in this case, any other thread cannot execute the non-synchronized block of code.
I tested a sample of code which I would like to share here and would like to know other comments.

public class SynchronizeTest implements Runnable
{
public static void main(String[] args)
{
SynchronizeTest obj = new SynchronizeTest();
Thread t1 = new Thread(obj);
t1.start();
obj.non_sync();
SynchronizeTest obj2 = new SynchronizeTest();
obj2.non_sync();
}
public void run()
{
sync();
}
public synchronized void sync()
{
System.out.println("Sync: " + Thread.currentThread().getName());
non_sync();
}
public void non_sync()
{
try
{
Thread.sleep(5000);
}
catch(InterruptedException e)
{

}
System.out.println("Non-Sync: " + Thread.currentThread().getName());
}
}


Here, when the Thread-0 goes to sleep, it takes all the monitors along with it so that even the main thread cannot enter the non-synchronized code. The main thread prints the non-synchronized block only after the worker thread dies.

More comments are welcome on this.
Graeme Jenkinson
Greenhorn

Joined: Sep 08, 2008
Posts: 27
Don is correct. The non-synchronised function is ... well non-synchronised.

Paul, I am not sure what you mean when you say:
it acquires a lock on the non-synchronized code as well
as this makes no sense to me. Nor am I sure what your code is attempting to show. However, I played about with it a bit and by adding a print statement on entry and exit to the non_sync method it is clear that with both the main thread and the user thread t1 call the method non-sequentially.

thanks,

Graeme
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
yes don is correct.
we can check that from the code posted by paul.


if we are calling non-sync method from sync method while thread is running
another thread can execute non-sync method it wont wait....
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
I am sorry about my previous post.
I was incorrect.. Non-synchronized blocks don't have any locks.

And the non-synchronized block can be concurrently accessed by other threads too when a particular thread is already accessing it.


For example when we run the following code:



Before the sleep of main method(just a case when main executes first before Thread-0), we get this output:

Entry Non-Sync: main
main going to sleep...
Sync: Thread-0
Entry Non-Sync: Thread-0
Thread-0 going to sleep...


This shows that: while the main has entered the non-sync method and is currently executing the sleep statement and is sleeping, Thread-0 also enters the non_sync method. This is shown by the output: Entry Non-Sync: Thread-0. This clearly shows that both the main and Thread-0 are concurrently executing the same method non-sync at the same time.
Hope this clears all the confusion.
Thanks.
[ October 16, 2008: Message edited by: Paul Somnath ]
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

package smalltesting;

public class ThreadSynchTest implements Runnable {
int counter = 0;
public static void main(String[] args) {
ThreadSynchTest ts = new ThreadSynchTest();
Thread t = new Thread(ts);
t.start();
ts.non_sync();
}

public void run() {
sync();
}

public synchronized void sync() {
System.out.println("synch---> " + Thread.currentThread().getName());
non_sync();
}

public void non_sync() {
for(int i=0;i<100000;i++)
{
counter = counter + 1 ;
}
System.out.println("counter :" + counter);
System.out.println("non synch entry---> " + Thread.currentThread().getName());
}
}



synch---> Thread-0
counter :147711
non synch entry---> Thread-0
counter :198808
non synch entry---> main



Yes I got it,

Then non synchronized methods which is called from syncronized code can be run with two thread at the same time, so there is no lock mechanism for that

Thank you friends for your answers and opinions it really helps...

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronize method call