• 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

Thread

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



Here the output is fixed, right? As we it is synchronized with a String reference s??

But If I replace synchronized(new String("s")), then Output becomes unpredictable, right?
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s="hello"; creates a String instance in the String pool, which can be reused.

But new String("hello") always create new instances.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi vijay wrote:Here the output is fixed, right? As we it is synchronized with a String reference s??

But If I replace synchronized(new String("s")), then Output becomes unpredictable, right?



Output is not fixed. It differs from one JVM to another. The thread t1 might go to runnable, but unless the thread scheduler pulls out the thread t1 from runnable pool to be thread of execution, the output is not fixed. It might be possible that the thread t2 might gain the access to be in running state before t1. So, my opinion. No the output is not fixed.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't we assume the fixed output, considering the String constant pool concept here. As the block in the run method synchronizes on a String object which will be in the constant pool (two instances of Q010, q1 & q2 shares it)?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are both right . Both threads are synchronizing their execution on a single String object. Add this to the end of main():
System.out.println("q1.s and q2.s are " + (q1.s == q2.s ? "the same object" : "different"));
So that means there are 2 possible outputs, depending on which thread starts running first.
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Treimin Clark wrote:String s="hello"; creates a String instance in the String pool, which can be reused.

But new String("hello") always create new instances.



String constant pool is not a collection of String objects, it's a collection of references to String objects.
For further reference visit String Constant Pool
 
Goodbye moon men. Hello tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic