• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronized method within a non-sync. method

 
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a situation.
I have a non-synchronized method in a class that uses a LinkedList object whose methods are synchronized using Collection.synchronizedList().Now if i use two threads and try
to access the non-sync. method of the class in the run method,can i say that although the threads will have concurrent access to the non-sync method of the class,but as soon as they hit the sync. method inside the non-sync method,they obtain a loock on the linkedlist object?
The code goes here-





now,will t1 and t2 acquire an exclusive lock on the linked list object?

Please reply...
Thanks...
 
Ranch Hand
Posts: 58
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, the t1 and t2 threads invoke the removeFirst() method using the NameList object nl. Therefore, the threads will attempt to acquire a lock on nl and not on the LinkedList object.
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Suhrid,
you are right.But i am sorry i posted the wrong code.Actually the add and removeFirst methods were not sync. inside the class.This was what i wanted to ask ,that if i include a sync. method inside non-sync. method then what will happen?
According to me,then t1 and t2 will lock the linked list object.

Please have a look again at my problem...
Thanks...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sudhanshu Mishra wrote:Thanks Suhrid,
you are right.But i am sorry i posted the wrong code.Actually the add and removeFirst methods were not sync. inside the class.This was what i wanted to ask ,that if i include a sync. method inside non-sync. method then what will happen?
According to me,then t1 and t2 will lock the linked list object.



Don't know what you changed... if anything. But the current version, locks the NameList instance that contains the LinkedList instance. There is no lock on the LinkedList instance.

BTW, in the future, it would be a good idea to reply with the new code, instead of replacing it -- it will make the current discussion moot if you change the first post.

Henry
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was the original code:

import java.util.*;
public class NameList {
private List names = Collections.synchronizedList(new LinkedList());
public void add(String name) {
names.add(name);
}
public String removeFirst() {
if (names.size() > 0)
return (String) names.remove(0);
else
return null;
}
}



public static void main(String[] args) {
final NameList nl = new NameList();
nl.add("Ozymandias");
class NameDropper extends Thread {
public void run() {
String name = nl.removeFirst();
System.out.println(name);
}
}
Thread t1 = new NameDropper();
Thread t2 = new NameDropper();
t1.start();
t2.start();
}
 
Suhrid Karthik
Ranch Hand
Posts: 58
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sudhanshu Mishra wrote:This was the original code:





Please remember to place code inside code blocks, like above. Now, since you are using a synchronized list, calls to methods such as size() and remove() will be synchronized. t1 and t2 cannot execute those methods concurrently. As to your question on which object they will lock on, it depends on the implementation of the synchronized list. In this case the class returned by the Collections.synchronizedList() method ensures synchronization by using a synchronized block on a lock object. So, t1 and t2 will lock on that object. Instead, if the methods were to be marked as synchronized, then the List object itself would be used as the lock.
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic