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


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

Re: a simple multi-thread question

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!feeder2.news.elisa.fi!uutiset.elisa.fi!7564ea0f!not-for-mail
From Jukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: a simple multi-thread question
Organization N/A
References <60c8877a-8321-4ba5-ab9a-8ccabd77874b@googlegroups.com>
X-no-archive yes
Content-Transfer-Encoding 8bit
Message-ID <lv4mra65p1.fsf@saunalahti.fi> (permalink)
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock sha1:DIEUZXOuzgQI7xZqzzljM0BGizI=
MIME-Version 1.0
Content-Type text/plain
Lines 42
Date Wed, 28 Jan 2015 21:55:22 +0200
NNTP-Posting-Host 91.155.88.191
X-Complaints-To newsmaster@saunalahti.com
X-Trace uutiset.elisa.fi 1422474922 91.155.88.191 (Wed, 28 Jan 2015 21:55:22 EET)
NNTP-Posting-Date Wed, 28 Jan 2015 21:55:22 EET
Xref csiph.com comp.lang.java.programmer:30948

Show key headers only | View raw


John <xsli2@yahoo.com> writes:

> synchronized private void print()
> {
>      System.out.println("printing A");
>      System.out.println("printing B");
>      System.out.println("printing C");
> }

Like others said, this will lock on the individual object, the instance
of the class that defines the print() method, and if you have two
instances of that class, they can execute the method without blocking
each other. And one way of preventing different threads is to make the
method static.

However, if you need to use instance variables in the method or
for some other reason can't make the method static, you can define a
static lock object and synchronize a code block on that, as long as the
threads run on the same JVM.
This way (I didn't test this snippet now, so there may be some bug, but
it should work something like this):

private static final Object synchroLock = new Object();

private void print() {
  // possibly some code that will not be synchronized
  synchronized (synchroLock) {
    System.out.println("printing A");
    System.out.println("printing B");
    System.out.println("printing C");
  }
  // possibly some more code that will not be synchronized
}

You must make the synchroLock object static, so it CAN be shared with
all instances of the class, and you should make it final to make sure
that it can't be instantiated after the classloader instantiates it.
If the synchroLock gets instantiated again, the synchronization will not
work between class instances having separate instances of synchroLock.

-- 
Jukka Lahtinen

Back to comp.lang.java.programmer | Previous | Next — Previous 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