@Henry
One major advantage of immutable classes is that they are thread safe and
Could be shared between multiple threads.

Can you explain the below concept of read only with a java example..i did
understand the theory part but can you please cite a java example.

How should the valueCounter be made read only so that thread A and thread B
only read the current value but do not update it. ???

Regards,
Deepak
On Jul 6, 2015 12:35 PM, "Deepak A L" <deepakl.2...@gmail.com> wrote:

> Thanks for your reply henry.i will be thankful if u can reply to below
> question.
>
> @Henry.@Sang Shin
> It could be easier to understand the below concept with a simple working
> java example.
>
> One major advantage of immutable classes is that they are thread safe.
> Could be shared between multiple threads. ----can you explain this point
> with a simple java example ??
>
> Regards,
> Deepak
> On Jul 5, 2015 9:31 PM, "henry joe" <kano...@gmail.com> wrote:
>
>> Hi,
>>
>>
>> Another Clarification
>>
>> *One major advantage of immutable classes is that they are thread safe.
>> Could be shared between multiple threads. ----can you explain this point
>> with a java example ??*
>>
>> I can't just come up with an example but here is a further explanation.
>> Another way to think of an immutable classes is that they are read-only .
>> That is you can only read them but can't modify them. That was the purpose
>> of making them immutable in the first place. In multithreaded application,
>> the situation arise that there is a race condition when two threads try to
>> access a common shared resources at the same. Supposing thread A has to
>> check the value of the valueCounter and based on the value do something.
>> So, let's say :
>>
>> if(valueCounter==0) //add 1 valueCounter+=valueCounter;
>>
>> Now, since the thread scheduling algorithm can swap between threads at
>> any time. Thread B could be given the opportunity by the scheduling
>> algorithm to access that mutable variable valueCounter and change the
>> value. So, imagine this scenario:
>>
>> thread A is scheduled, checked that the value of valueCounter  is 0 but
>> just as it was about to add 1 to it, thread B is suddenly given the spot by
>> the thread scheduler, which just increments the value of valueCounter to 1.
>> Now, thread A is given the spot again but this time, the valueCounter is no
>> more 0 but 1. So, instead of return 1, it returned 2. which is a wrong
>> result for the user.  Ok, this is just an example off my head but you get
>> the idea. Now, the reason for this is because that variable valueCounter is
>> not read-only. If it were read-only, then A will only read it but won't
>> update it, B will also read it and won't update it because you can't update
>> it. Hope this clarifies it further.
>>
>>
>> *In Question. 2 how do i instantiate MyOwnImmutableClass. how do i run
>> this program. ??*
>>
>> Just as you would instantiate a non immutable class.
>>
>> public static  void main(String[] args){
>>
>> final MyOwnImmutableClass myOwnImmutableClass = new
>> MyOwnImmutableClass();
>>
>>
>> *}*
>>
>>
>> Regards,
>> Deepak
>>
>> On Sun, Jul 5, 2015 at 12:33 PM, Deepak A L <deepakl.2...@gmail.com>
>> wrote:
>>
>>> Another Clarification
>>>
>>> One major advantage of immutable classes is that they are thread safe.
>>> Could be shared between multiple threads. ----can you explain this point
>>> with a java example ??
>>>
>>> In Question. 2 how do i instantiate MyOwnImmutableClass. how do i run
>>> this program. ??
>>>
>>> Regards,
>>> Deepak
>>> On Jul 5, 2015 2:55 PM, "Deepak A L" <deepakl.2...@gmail.com> wrote:
>>>
>>>> In Question. 2 how do i instantiate MyOwnImmutableClass. how do i run
>>>> the program ??
>>>>
>>>> Regards,
>>>> Deepak
>>>> On Jul 5, 2015 2:38 AM, "henry joe" <kano...@gmail.com> wrote:
>>>>
>>>>> *1. What is Java Immutable class.?*
>>>>>
>>>>> An immutable class is one whose object of that class is created, it
>>>>> cannot be modified
>>>>>
>>>>>
>>>>> *2. Write a java Immutable class.*
>>>>>
>>>>> public final class MyOwnImmutableClass{
>>>>>
>>>>> private final name;
>>>>>
>>>>> public String getName(){return name;}
>>>>>
>>>>> }
>>>>>
>>>>> That is it! To make a class immutable, ensure the class is final,
>>>>> hence cannot be extended, the fields are final hence cannot be modified 
>>>>> and
>>>>> no setter method should be provided to such class.
>>>>>
>>>>>
>>>>>
>>>>> *3. Advantages of java Immutable class.*
>>>>>
>>>>> One major advantage of immutable classes is that they are thread safe.
>>>>> Could be shared between  multiple threads
>>>>>
>>>>>
>>>>> *4. Disadvantages of java Immutable class. *
>>>>>
>>>>> In extreme cases, they could slow down . Read this stack overflow for
>>>>> more on this ==>
>>>>> http://stackoverflow.com/questions/752280/downsides-to-immutable-objects-in-java
>>>>>
>>>>>
>>>>> *5. Any impact of Java Immutable class on Performance ? *
>>>>>
>>>>> Well, both the advantageous and disadvantageous characteristics impact
>>>>> your class performance.
>>>>>
>>>>>
>>>>>
>>>>> *6.If you have any other points on Java Immutable class.please advise.*
>>>>>
>>>>> My advice, might not worth it, I haven't seen this used in most of my
>>>>> coding. So, I can't advice you much on it. Perhaps, other more experienced
>>>>> developers could shed more light on practical application of immutable
>>>>> classes. I only use the String and wrapper classes in Java for my needs.
>>>>> Never bothered to write mine in real applications.
>>>>>
>>>>>
>>>>> On Fri, Jul 3, 2015 at 6:48 PM, Deepak A L <deepakl.2...@gmail.com>
>>>>> wrote:
>>>>>
>>>>>> hi pple
>>>>>> i have below queries
>>>>>>
>>>>>> 1. What is Java Immutable class.?
>>>>>> 2. Write a java Immutable class.
>>>>>> 3. Advantages of java Immutable class.
>>>>>> 4. Disadvantages of java Immutable class.
>>>>>> 5. Any impact of Java Immutable class on Performance ?
>>>>>> 6.If you have any other points on Java Immutable class.please advise.
>>>>>>
>>>>>> Regards,
>>>>>> Deepak
>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "JPassion.com: Java Programming" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to jpassion_java+unsubscr...@googlegroups.com.
>>>>>> Visit this group at http://groups.google.com/group/jpassion_java.
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>>
>>>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"JPassion.com: Java Programming" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jpassion_java+unsubscr...@googlegroups.com.
Visit this group at http://groups.google.com/group/jpassion_java.
For more options, visit https://groups.google.com/d/optout.

Reply via email to