Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30948
| 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> |
| Message-ID | <lv4mra65p1.fsf@saunalahti.fi> (permalink) |
| Date | 2015-01-28 21:55 +0200 |
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
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