Re: Simple(?) synchronized() block question



noident@xxxxxxxxxxx wrote:
public class Foo extends Thread

Extending Thread is almost always a bad idea. If your book tells you to do that, burn it.

{
private String s;
public Foo(String ss) { s = ss; }

public void run()
{
synchronized(s) // changing this line to 'synchronized(this)' fixes
the problem

Using a client supplied String for a lock isn't a great idea. The String may be shared with the rest of the code in the JRE.

A handy little trick is to declare a nested or local class called Lock with an empty body. The full name of classes are shown if you use ctrl-\, ctrl-break, jstack or similar. A custom class name will help you identify locks.

{
try
{
wait();

Even if the Thread was the right object to wait on, Thread is a bad choice of lock. In Sun JVMs, Thread uses itself as a lock for internal operations. Attempting to lock it yourself may give strange results.

You need to be careful what you synchronise on. An easy mistake is to lock on an outer class instance, and then within in an inner class lock on the inner class instance because this is different.

}
catch(InterruptedException e)
{
System.err.println(e);
System.exit(1);

Seems a little extreme...

}
}
}

public static void main(String[] args)
{
new Foo("abc").start();
}
}

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
.



Relevant Pages

  • Re: [PATCH] tracing/lockdep: turn lock->name into an array
    ... the "lock acquired" event is traced using a TRACE_EVENT. ... But we can't use the char * type for the name without risking to ... I guess it only happend at module unloading. ... make delayed string table freeing at module unloading. ...
    (Linux-Kernel)
  • Re: Thread Locking In Static Methods - How?
    ... the lock keyword is a terribly named keyword ... Because you use a string literal, anyone else locking the same string literal will be in contention with you as the string is interned ... What this does is cause a lock of any code location that uses the ...
    (microsoft.public.dotnet.languages.csharp)
  • IM Lock 2006 - Insecure Registry Permission Vulnerability
    ... IM Lock 2006 discloses passwords to local users. ... (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) ... Dim GetCrypt, Decrypt As String ... Dim lResult As Long ...
    (Bugtraq)
  • Re: Floyd Rose tuning stability
    ... No string lubricants or anything like that. ... If you're able to test the pitch of the string section between the locking ... screws coming out the back that lock the strings in, ... Strat-style whammy bar. ...
    (alt.guitar)
  • Re: Thread Locking In Static Methods - How?
    ... Because you use a string literal, anyone else locking the same string literal will be in contention with you as the string is interned ... What this does is cause a lock of any code location that uses the ... > create a private static member variable for the specific method, ...
    (microsoft.public.dotnet.languages.csharp)