|
>>Any one looking for a short course on this should study java.lang.String >>and java.lang.StringBuffer and look around for explanations of "immutable >>object" in the literature. >Is this because all pass by reference variables are locked ( single >threaded ) before update? I am a C++ bigot and all I know about java is >that .NET may marginalize it in the years to come <g>. But I am interested >in how Java implements built in multi threading. >thanks, >Steve Richter No, it doesn't work that way at all. The lock never applies to variables -- it only applies to instances. For methods, the locking is always in terms of the "this" object whose synchronized method is entered. For the synchronize verb, it is the object whose reference is specified that is locked. That's the lot of locking in Java. So, that means parameters' objects are not locked simply by being in a method call. The more interesting features are kind of implied and kind of not. For instance, the "new" command (build an object from the heap, very like C++) is inherently thread safe because only one thread is building the object (unless you define a _very_ unusual constructor) and no one else knows about it yet. What happens if you _define_ an object such that its contents _never change_ after the "new" (constructor) finishes? Answer: It is automatically thread safe and everyone and his brother can have references to it without any synchronization. That's an immutable object. Very relevantly, "String" is an immutable object and so is always thread safe. Immutable objects are not really possible in C++ thanks to the ever-present possibility of "well defined casting abuse" (un-const-ing) but Java is strict enough in its semantics to have them. There's a lot more to this than I can do justice to in this newsgroup. But, there's a literature about it that can be discovered somewhere or other. Once I was tuned into it, I found out about it rather readily -- just can't remember exactly where offhand. Larry W. Loen - Senior Linux, Java, and iSeries Performance Analyst Dept HP4, Rochester MN
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.