Re: synchronized statements
From: FISH (joeking_at_merseymail.com)
Date: 11/05/03
- Previous message: Glen Herrmannsfeldt: "Re: Travelling Salesman problem..any known java source code?"
- In reply to: Jonas Kongslund: "Re: synchronized statements"
- Next in thread: Jonas Kongslund: "Re: synchronized statements"
- Reply: Jonas Kongslund: "Re: synchronized statements"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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- ><>
- Previous message: Glen Herrmannsfeldt: "Re: Travelling Salesman problem..any known java source code?"
- In reply to: Jonas Kongslund: "Re: synchronized statements"
- Next in thread: Jonas Kongslund: "Re: synchronized statements"
- Reply: Jonas Kongslund: "Re: synchronized statements"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|