• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Threads - synchronized methods

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A extends Thread {
String[] sa;
public A(String[] sa) {this.sa = sa;}
public void run() {
synchronized (sa) {System.out.print(sa[0] + sa[1] + sa[2]);}
}}
class B {
private static String[] sa = new String[]{"X","Y","Z"};
public static void main (String[] args) {
synchronized (sa) {
Thread t1 = new A(sa); t1.start();
sa[0] = "A"; sa[1] = "B"; sa[2] = "C";
}}}

The output here is ABC and the explanation is that the t1 thread will be blocked until main method execution completes.I agree that the t1 thread is invoking a block synchronised on sa but that block is in a different class i.e class A???
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

There can be several reasons for this:

1) Arrays ar objects so they are passed by reference and they reflect any changes.

2) Thread does not start execution immediately after calling the start method.

Please tell me if I am wrong or missing out on something.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by janki tangeda:
I agree that the t1 thread is invoking a block synchronised on sa but that block is in a different class i.e class A???



Yes, they are different classes, but they synchronize on the same object: the array that is passed into the other class. Note that this is not a synchronized method, but a synchronized(...){} statement.

Regards, Olaf
 
janki tangeda
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that this is not a synchronized method, but a synchronized(...){} statement

Why this statement?what would happen if it were a synchronized method?
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by janki tangeda:
Note that this is not a synchronized method, but a synchronized(...){} statement

Why this statement?what would happen if it were a synchronized method?



Methods are synchronized on the this object if they are non-static or on the class object if they are static while code blocks can be synchronized on any object. So if the run method was synchronized instead of the block inside the run method then t1 would only need to acquire its own lock to run whereas now it need to wait for the block inside the main method to release the lock on sa.
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic