@Henry,can you provide a complete java working example for Immutable class
??
import MyOwnImmutableClass;
public static void main(){ MyOwnImmutableClass myOwnImmutableClass = new
MyOwmImmutableClass(); int final valueIsImmutable =
myOwnImmutbaleClass.getValue(); System.out.println(valueIsimmutable);
Thread A = new Thread("Thread A"){
public void run(){ valueIsImmutable = 7; //trying to change the value will
not work try{ Thread.sleep(100); System.out.println("new value is":
valueIsImmutable); }cathch(InterruptedException e) { //do whatever you wish
to do with expception here.. } } };
Thread B = new Thread("Thread B"){ public void run(){ valueIsImmutable = 9
//trying to change it as well here.. this will not work.. try{
Thread.sleep(100); System.out.println("new value is": valueIsImmutable);
}cathch(InterruptedException e) { //do whatever you wish to do with
expception here.. }
} }; A.start(); B.start(); }
Regards,
Deepak
On Jul 8, 2015 1:31 PM, "henry joe" <[email protected]> wrote:
> Hi Deepak,
> Ok. Will do that when am free today. Kindly share the code when I have
> sent it on the java passion public forum so others can review and add or
> study it.
> Cheers
> On 8 Jul 2015 10:54, "Deepak A L" <[email protected]> wrote:
>
>> Hi Henry,
>> Due to interim power cuts.i have not been able to try out
>> the code which u sent.
>> I'm sending email from phone.
>>
>> In case you are free.please do send a working example of Immutable class
>> !!!
>>
>> Thnks for your efforts ;-) :-)
>>
>> Regards,
>> Deepak
>> On Jul 7, 2015 11:49 PM, "henry joe" <[email protected]> wrote:
>>
>>> Hi Deepak, I am not on watzapp at the moment but I will be a bit free
>>> tomorrow. I wil write a full working example. Did you try the code I sent
>>> you? Did it complain?
>>> On 7 Jul 2015 21:15, "Deepak A L" <[email protected]> wrote:
>>>
>>>> Are you on whatsApp ?? then message me your number..i will add you
>>>>
>>>> Regards
>>>> Deepak
>>>> On Jul 7, 2015 3:43 PM, "henry joe" <[email protected]> wrote:
>>>>
>>>>> Hi Deepak,
>>>>>
>>>>> *import MyOwnImmutableClass;*
>>>>>
>>>>> *public static void main(){*
>>>>> *MyOwnImmutableClass myOwnImmutableClass = new MyOwmImmutableClass();*
>>>>> *int final valueIsImmutable = myOwnImmutbaleClass.getValue();*
>>>>> *System.out.println(valueIsimmutable);*
>>>>> *Thread A = new Thread("Thread A"){*
>>>>>
>>>>> *public void run(){*
>>>>> * valueIsImmutable = 7; //trying to change the value will not work*
>>>>> *try{*
>>>>> * Thread.sleep(100);*
>>>>> *System.out.println("new value is": valueIsImmutable);*
>>>>> *}cathch(InterruptedException e)*
>>>>> *{*
>>>>> *//do whatever you wish to do with expception here..*
>>>>> *}*
>>>>> *}*
>>>>> *};*
>>>>>
>>>>> *Thread B = new Thread("Thread B"){*
>>>>> *public void run(){*
>>>>> *valueIsImmutable = 9 //trying to change it as well here.. this will
>>>>> not work..*
>>>>> *try{*
>>>>> * Thread.sleep(100);*
>>>>> *System.out.println("new value is": valueIsImmutable);*
>>>>> *}cathch(InterruptedException e)*
>>>>> *{*
>>>>> *//do whatever you wish to do with expception here..*
>>>>> *}*
>>>>>
>>>>> *}*
>>>>> *};*
>>>>> *A.start();*
>>>>> *B.start();*
>>>>> *}*
>>>>>
>>>>> Try this code. I have not tested it. Make a constructor in the
>>>>> MyOwnImmutable class and pass in the value. and get that value and see if
>>>>> the value can be changed in the 2 threads A and B. I am sorry, abit very
>>>>> busy. Planning on travelling tomorrow and have been parking alot.
>>>>>
>>>>> Regards
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> On Tue, Jul 7, 2015 at 12:14 PM, Deepak A L <[email protected]>
>>>>> wrote:
>>>>>
>>>>>> @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" <[email protected]> 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" <[email protected]> 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 <[email protected]
>>>>>>>> > 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" <[email protected]>
>>>>>>>>> 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" <[email protected]> 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 <
>>>>>>>>>>> [email protected]> 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 [email protected]
>>>>>>>>>>>> .
>>>>>>>>>>>> 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 [email protected].
Visit this group at http://groups.google.com/group/jpassion_java.
For more options, visit https://groups.google.com/d/optout.