Re: synchronized statements

From: FISH (joeking_at_merseymail.com)
Date: 11/05/03

  • Next message: Rajatheone: "Salute to all Java Gurus (?=JTapi)"
    Date: 5 Nov 2003 02:37:45 -0800
    
    

    Jonas Kongslund <dont@mail.me.at.all> wrote in message news:<XiYpb.7603$yq2.1828@news.get2net.dk>...
    > John Thorner wrote:
    > > - I would like to print all the System.out.println() statements that are
    > > related to each other (as shown above) one right after the other for
    > > the same thread. That is, I want the System.out.println() statements
    > > to be printed together for the same thread they belong to.
    >
    > A quick solution not involving the synchronized keyword is to print only one
    > string per thread, i.e. concatenate the strings.

    This doesn't stop the printing of one set of strings interrupting the
    printing of another set - I don't think System.out has any kind of
    locking mechanism to ensure two overlapping calls to println cannot
    interleave their output on the console. (Unless someone knows better?)

    I'm surprised acquiring the lock on System.out didn't work. At first
    glance I'd expect that to do the job, as all threads which entered that
    synchronized block would be required to own the lock on System.out .
    If this genuinely doesn't work (perhaps there's something lurking within
    System.out.println which releases the lock?) then the alternative is to
    replicate the behaviour with your own object. Just create a singleton
    object (a static member will do) and lock on that instead.

    -FISH- ><>


  • Next message: Rajatheone: "Salute to all Java Gurus (?=JTapi)"

    Relevant Pages

    • Re: synchronized using String.intern()
      ... would obviously not lock the file from access by another process, ... In my case, the number of such strings is guaranteed to be on the order of 1-10, so, again, I don't care. ... ADD ENTRY TO FILE GIVING FILE ... public void doSomeOtherLowLevelOperationOfYourApplication() { ...
      (comp.lang.java.programmer)
    • Re: A C showstopper
      ... change unless you call them inside a lock. ... strings down for an unpredictable length of time, ... the same problem whatever language you are using. ... Flash Gordon- Hide quoted text - ...
      (comp.lang.c)
    • Re: A C showstopper
      ... change unless you call them inside a lock. ... strings down for an unpredictable length of time, ... That is a reader thread can take a reference to a shared string ... the same problem whatever language you are using. ...
      (comp.lang.c)
    • Re: synchronized method
      ... > Guards method execution, similar to Java's synchronized keyword. ... Under normal execution the lock will not be released because you return ... The calling thread may acquire the lock multiple times, ...
      (comp.lang.python)
    • Re: A C showstopper
      ... change unless you call them inside a lock. ... strings down for an unpredictable length of time, ... is a simple COW-based solution to the classic reader/writer problem. ... That is a reader thread can take a reference to a shared string without worry of interference form writers such that they can read the string without synchronization. ...
      (comp.lang.c)