Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30915

Re: a simple multi-thread question

Newsgroups comp.lang.java.programmer
Date 2015-01-26 07:54 -0800
References <60c8877a-8321-4ba5-ab9a-8ccabd77874b@googlegroups.com>
Message-ID <3958fd06-9f61-456a-9cee-4e8daaca9a43@googlegroups.com> (permalink)
Subject Re: a simple multi-thread question
From Robert Klemme <shortcutter@googlemail.com>

Show all headers | View raw


On Monday, January 26, 2015 at 4:40:51 PM UTC+1, John wrote:
> Hi:
> 
> I am amazed at what I saw:
> 
> synchronized private void print()
> {
>      System.out.println("printing A");
>      System.out.println("printing B");
>      System.out.println("printing C");
> }
> 
> There were two threads running. I was expecting to see:
> 
> printing A
> printing B
> printing C
> printing A
> printing B
> printing C
> 
> However, I saw this:
> 
> printing A
> printing A
> printing B
> printing C
> printing B
> printing C
> 
> I saw:
> printing APrinting A
> 
> printing B
> printing B
> printing C
> printing C
> 
> I thought the method is atomic. Is that because the byte stream to show up on the linux terminal is messed up in an unpredicatable way?

It's an instance method and not a class method (missing "static" keyword).  You probably created two instances, one for each thread.  But then, each gets their own mutex.  Which makes the mutexes moot.

Cheers

robert

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

a simple multi-thread question John <xsli2@yahoo.com> - 2015-01-26 07:40 -0800
  Re: a simple multi-thread question Arne Vajhøj <arne@vajhoej.dk> - 2015-01-26 10:53 -0500
  Re: a simple multi-thread question Robert Klemme <shortcutter@googlemail.com> - 2015-01-26 07:54 -0800
  Re: a simple multi-thread question Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-01-26 10:55 -0500
    Re: a simple multi-thread question Robbie Brown <rob@example.com> - 2015-01-26 18:05 +0000
      Re: a simple multi-thread question Robbie Brown <rob@example.com> - 2015-01-26 18:08 +0000
        Re: a simple multi-thread question Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-01-26 17:19 -0500
      Re: a simple multi-thread question Arne Vajhøj <arne@vajhoej.dk> - 2015-01-26 13:27 -0500
        Re: a simple multi-thread question Robbie Brown <rob@example.com> - 2015-01-26 19:51 +0000
  Re: a simple multi-thread question Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2015-01-28 21:55 +0200

csiph-web