• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

synchronize method call

 
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, another thread can call the non-synchronized method
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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() !
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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've got no option but to sell you all for scientific experiments. Or a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic