• 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

how and why to do it ?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Threadtest
{ static StingBuffer str_1=new StringBuffer("");
static StingBuffer str_2=new StringBuffer("");
public static void main(String args[]);
{final String str="";
new Thread()
{pubic viod run()
{synchronized(str)
{str_1.append("A");
str_2.append("B");
System.out.priintln(str_2);
System.out.priintln(str_1;
}
}
}.start();
new Thread()
{pubic viod run()
{synchronized(str)
{str_2.append("C");
str_1.append("D");
System.out.priintln(str_1);
System.out.priintln(str_2);
}
}
}.start();
}
}
what may be the output of this code?
A.ABBCAD
B.BCBADD
C.CDDACB
D.BAADBC
E.DCDACB
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the million-dollar question is?
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer should be D and F, where F is "the answer is un-determined".

Nick
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




After fixing the typos: BAADBC or DCCBDA
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike,

I ran the code and the output is BAADBC .Why not DCDACB ?

Thanks
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The execution sequence of threads is controlled by OS. Different OS has different handling, and thus, even, say, Thread 1 starts earlier than Thread 2 does not necessary means Thread 1 will be 1st executed.

Thus, the former answer is obtained when the 1st thread is executed first, while the 2nd thread is executed afterwards.

In case, an OS schedules 2nd thread to be executed first and then the 1st thread, you will get the latter answer.

That's why I said, the result is un-determined, as it depends on how the OS schedule the execution.

For details, please read Kathy and Bert's book.

Nick
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I ran the code and the output is BAADBC .Why not DCDACB ?


Just reverse the order of the two "new Thread ... " statements and you'll usually get the other answer.
 
yizao zhang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much.
 
reply
    Bookmark Topic Watch Topic
  • New Topic