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

Inner Thread Classes

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class SyncTest {
public static void main (String [] args) {
final StringBuffer s1 = new StringBuffer ();
final StringBuffer s2 = new StringBuffer ();
new Thread () {
public void run() {
synchronized (s1) {
s2.append (�A�);
synchronized (s2) {
s1.append (�B�);
System.out.println (s1);
System.out.println (s2);
}
}
}
}.start();
public void run() {
synchronized (s2) {
s2.append (�C�);
synchronized (s1) {
s1.append (�D�);
System.out.print (s2);
System.out.print (s1);
}
}
}.start ();
}
}

I tried this program and the output was BAACBD. I tried it on both Windows and Linux . I removed the synchronized �methods� and tried again and it gave me the same output. Are these threads concurrent?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, Raef!

I guess you won't get much answers to this question because the code is unreadable and cannot compile.

Perhaps repost the code in a repaired version and use code tags.
At the posting page, mark the part of your posting that should be indented. Usually that will be your code (or some tabellaric output).

Hit the - Button below.
that's it!



and:
you wrote:


I removed the synchronized “methods” and tried again



Do you mean, you removed the synchronized keyword and tried again?

If so, you cannot always expect a change in behaviour, as the code in the run method is so short and performed so quickly, that even in a non-synch method no other thread has the chance to get selected by the scheduler.
Perhaps try to insert an additional call to yield() in that method.


Yours,
Bu.
 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanks a lot for all your responses. And you are right it didn't compile. And I mean synchronized keywords instead of methods.
 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the right code.



and the output is: BABCAD
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raef,
Could you please have a look at

Javaranch

We had a full length discussion on the same example and the issues with this class.
If you do not understand the explaination, please feel free to ask your questions.
Thanks
Deepak
[ August 07, 2007: Message edited by: Deepak ]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic