• Post Reply Bookmark Topic Watch Topic
  • New Topic

2 synchronized methods calling a non synchronized method  RSS feed

 
Vib Mator
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i have a program

which has two synchronized methods.
but both make a call to same non synchronized method.
this non synchronized method makes changes to the list of data that i have.
is it thread safe??

synchronized method1(obj o1){

//some other data
makeFirst(o1)
}

synchronized method2(obj o2){

//some other data
makeFirst(o2)
}

makeFirst()
{

makes changes to same list of data

}


printdata()
{iterate over list and print values
}

do i need to make 'makechange() and printdata()' methods synchronized as well ???




makeFirst is a private method...which is just called by these two methods.

while the PrintData is called publicly.

wouldnt i require to synchronize this makeFirst method because at a time when both synchronized methods will be calling this method.
the two different threads may each may be calling one of the methods

like
Thread1 calls synchronized method1
Thread2 calls synchronized method2

but these two methods call the makeFirst method. wihch is accessing the same list of data.
there is nothign that is synchronizing access to the makeFirst method.
ie. nothing is making these two calls from two threads to be synchronized.

just a psuedocode below.

thread1.synchronized method1.makeFirst()
thread2.synchronized method2.makeFirst()

both sharing same data and making changes to it..

but if i make makeFirst as synchronized, it will make only one of these methods to access the data and make changes to the list at one instance of time.
Am i thinking on the right way.?




 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vib Mator wrote:
makeFirst is a private method...which is just called by these two methods.


wouldnt i require to synchronize this makeFirst method because at a time when both synchronized methods will be calling this method.
the two different threads may each may be calling one of the methods

like
Thread1 calls synchronized method1
Thread2 calls synchronized method2

but these two methods call the makeFirst method. wihch is accessing the same list of data.
there is nothign that is synchronizing access to the makeFirst method.
ie. nothing is making these two calls from two threads to be synchronized.

just a psuedocode below.

thread1.synchronized method1.makeFirst()
thread2.synchronized method2.makeFirst()

both sharing same data and making changes to it..

but if i make makeFirst as synchronized, it will make only one of these methods to access the data and make changes to the list at one instance of time.
Am i thinking on the right way.?


Hint: can two threads running in two synchronized methods call the makeFirst() method .... well, first, is that even possible? Can a method1() and method2() be running concurrently?

Henry

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!