لمحة جافاوية 55: تحويل Collection من الوضع غير الآمن إلى الوضع الآمن

السلام عليكم

كيف حالكم يا شباب
اشتقت لكم كثيراً
لا أعلم هل الشعور متبادل
لكن الأخ عبده مصمم على أني مجرد حمار يضيع وقته في أشياء لا نفع لها
وأنه يجب علي أن أشرح علم حقيقي مثل .Net أو Sisco
وعلى الرغم من أني أخبره في كل مرة أنه شخص غير مرغوب به هنا
إلا أنه كالعادة يكون أول الموجودين
على كل منورنا يا عبده العبيط
(ها ماذا قلت؟)
أقول منورنا يا عبده الجميل
(اهااا حسبت)
دعوكم من هذا كله ولنتكلم عن الأمور التي جئنا لنتكلم عنها
لمحة اليوم تدور حول الحزمة Collection
معروف أن الجافا قامت ببناء جزء كبير من الحزمة بدون أن تدعمها بحماية ضد تعدد الخيوط أو ما يعرف بـ Thread safe
طبعاً الأسباب هي السرعة كالعادة
فالدوال والمناطق التي تحتوي على synchronized تكون بطيئة بالعادة
على كل هذا شيء جيد
لكني أحياناً أحتاج إلى Collection يكون محمي
بحيث لا أحمل هم مشاكل الخيوط
سيقول البعض ولماذا لا تستخدم الفئات البديلة مثل Vector و HashTable
ممممممممممممممممممممممم
حل جيد لكن مهلاً ليس في كل الحالات
خذوا عندكم هذه الحالة
أنا لا أحتاج Collection محمي إلا في جزء معين من البرنامج
فما الذي يجعلني أزيد حمل البرنامج الخاص بي باستخدام Vector و HashTable
(إذن ما الحل يا عبقري؟)
الحل يا أخ عبده تقدمه جافا في الفئة class المسماة Collection
جافا تقدم مجموعة من الدوال التي تعمل على تحويل Collection من الوضع غير الآمن Thread unsafe إلى الوضع الآمن Thread safe
والدوال كالتالي

public static  Collection synchronizedCollection(Collection c);
public static  Set synchronizedSet(Set s);
public static  List synchronizedList(List list);
public static  Map synchronizedMap(Map m);
public static  SortedSet synchronizedSortedSet(SortedSet s);
public static  SortedMap synchronizedSortedMap(SortedMap m);

يوجد عيب بين في هذه الدوال حيث تقتصر على الدوال الموجودة في الواجهات interface فقط مثل Map ,Set,…
بمعنى لو أنك كنت تستخدم HashMap وأردت تحويلها إلى synchronizedHashMap فستحصل فقط على synchronizedMap
وهذا يعني أنك لن تستطيع استخدام الدوال الموجودة في HashMap والغير موجودة في Map
على كل يمككنا التغاضي عن هذا العيب مقارنة بالفائدة التي نجنيها من هذه الدوال

حكمة اليوم:
العناوين الطويلة لا تدل بالضرورة على مواضيع طويلة
(تقصد استظراف اليوم)
لا أريد أن يصبح الموضوع طويلاً
لذا سمه ما شئت يا أخ عبده

تحياتي
المراجع:
1- مقالة من دروس موقع صن بعنوان: Wrapper Implementations
http://java.sun.com/docs/books/tutorial/collections/implementations/wrapper.html

Tags: , , , , , , , , , ,

8 تعليقات to “لمحة جافاوية 55: تحويل Collection من الوضع غير الآمن إلى الوضع الآمن”

  1. يقول Omar Al Kababji:

    Hi, nice topic, sorry for writing in english, I hope it will not be a problem

    I Think you are missing something important in this post, as you are saying you want to make your objects thread safe, but lets go back to another question, why should they be thread safe? In which part of your application synchronization issues could arise?

    Problem: my Collection is not thread safe because there are multiple threads reading/writing to it.

    Solution: Wrap the collection inside a class and make its access modifier private, and provide public methods to read and write to this collection and synchronize on this object.

    In many cases thread safety is moved to the programmer because if you want to make everything thread safe, you are moving from a multithreading environment to a single threaded environment which will kill performance and multi-tasking, and this is why collections are not thread safe, or servlets are not thread safe. because its a programmer responsibility 😉

    Nice Blog by the way.

  2. يقول admin:

    بالتأكيد ليس هناك مشاكل مع الإنجليزية
    مع أني أفضل الكتابة بالعربية

    أنا في هذه التدوينة لم أقصد تحويل الفئة class إلى وضع الأمان بالكلية
    كل ما قصدته تحويل جزئية منها ألا وهي الـ collection
    لضمان عدم حصول أية مشاكل في فترة زمنية ما من حياة البرنامج
    بعد انقضاء هذه الفترة ستصبح كون الـ collection في الوضع الآمن عبء على البرنامج لا أكثر

    ومن منطق الحفاظ على الأدائية العالية للبرامج خاصتي
    فأنا آخذ بالاعتبار هذه النقطة

    سأعطيك مثال على هذه الحالة
    أنا أعمل في مجال تصنيع الحزم packages
    أنا أعرف في داخل الحزمة الخاصة بي أن الـ collection الخاص بي
    لا يحتاج أي عمليات synchronization لأني أنا من قام بتصميم الحزمة
    طيب خذ عندك السناريو التالي
    ماذا لو أردت أن أعطي المبرمج المستخدم لحزمتي إمكانية التعامل مع الـ collection
    اهااااااا
    أنا الآن في وضعية لا أحسد عليها
    ويجب على أساسها أن أقرر إذا كنت أهتم بالحفاظ على الأمان الخاص بحزمتي
    أم أني سأتركها سداحاً مداحاً

    بالنسبة للدوال التي شرحتها في هذه التلميحة
    هي تقوم بالضبط باستخدام الآلية التي ذكرتها والتي تسمى wrapper pattern

    بالنسبة لكون collections غير آمنة ضد الخيوط
    فهذا القول غير صحيح بالمطلق هناك جزئيات من الحزمة collection
    تخضع للأمان منها Vector و HashTable
    يبقى الموضوع موضوع مقارنة حجم الاستفادة التي سأستفيدها من الوضع الآمن لكائناتي بالنسبة للخسارة

    أي تعليقات أو استفسارات أنا جاهز للمناقشة

    by the way I like the discussion with you

    تحياتي

  3. يقول Omar Al Kababji:

    I have two issues, Vector and Hashtable are old java classes and there are new Classes to replace them, (Hashtable is even with a small letter “t” it should be HashTable). anyway although Hashtable has almost all of its methods synchronized which causes performance issues (the same for Vector).

    about your example of your package using a Collection. if some programmer is going to use your packages he should access your framework through a well defined API, and this API is the one that should force synchronization and thread safety (if needed) on its public methods. the programmer should never be able to access the collection directly (encapsulation).

    One last thing I highly recommend not using Vector and Hashtable, if you really need thread safety then go with the method you provided and avoid using Vector and Hashtable, since using Collections is much faster compared to Vector and Hashtable.

  4. يقول admin:

    بالنسبة لكون Vector و Hashtable
    with small b 🙂
    فئات قديمة صحيح لكن هذا لا يعني أنها فئات ملغاة deprecated
    وأظن الفرق واضح

    أنا أؤيد الفقرة الخاصة بكون API هي الحكم بيني وبين المبرمجين المستخدمين لهذه الحزمة
    لكني معني في الأمر الأول والأخير بالمرونة العالية
    في هذا الموضوع بالذات أنا أؤيد مايكروسوفت وضد صن تماماً
    ما الفائدة في تصميم حزمة لا يستخدمها سوى الفئة ذات المعلومات القوية
    لاتنسى أن التعامل مع الخيوط Threads و مسائل التزامن synchronization بينها
    من الأمور الصعبة جداً

    بالنسبة لفقرة المبرمج يجب أن لا يكون له صلاحية على collection بشكل مباشر
    فهذه الجزئية صحيحة من الناحية التصميمية للحزمة
    لكن من الناحية العملية لا يمنع أن تحصل مثل هذه الحالة
    خذ عندك هذا المثال من الحزمة AAnimation الخاصة بي
    الوحدة الأساسية لدي هي الحركة Animation
    والتي تصف حركة للكائن المتحرك بين مجموعة حركات
    ولدي AnimationList والتي تحتوي على مجموعة حركات الكائن المتحرك
    أنا أعطي للمستخدم الإمكانية الكاملة للتحكم بمجموعة الحركات
    بحيث يمكن للمستخدم الإضافة والحذف كما يريد
    صحيح أن هذا الموضوع يكلفني كثيراً داخل الحزمة
    لكني ولكي أكون صادقاً معك أجده مفيداً جداً
    بحيث يمكن للمبرمج أن يضيف أي نوع من الحركات على الكائن المتحرك
    بناءاً على أحداث قد تحصل خارج بيئة التحريك ككل

    هذه الجملة قد لا أجد ما يبررها بشكل
    One last thing I highly recommend not using Vector and Hashtable, if you really need thread safety then go with the method you provided and avoid using Vector and Hashtable, since using Collections is much faster compared to Vector and Hashtable.
    لأن سبب البطأ في Vector و Hashtable هي عمليات التزامن synchronization
    لا أكثر ولا أقل

    تحياتي

  5. يقول Omar Al Kababji:

    Yes you are right you could do it your way, but I prefer make access to my internals through my own API, I don’t know what is your AAnimation project but if I was going to implement a moving action, I would provide for example a method like this move(int x, int y); or base my API on Events, but I can’t judge since I don’t know what you are doing exactly.

    Just to make it clear, what I meant is that instead of using Vector which has all of its methods synchronized, I would use an ArrayList or even an Array if possible, and make access to it through a synchronized method (or synchronize on the object itself).

    Bottom line: I will have only one synchronized method, instead of all the synchronized methods of Vector. got my point? I am not saying that you should not use Vector and Hashtable, but I prefer avoiding them.

    One final thing, programming is not meant to be easy, and there are really few good programmers around, but if someone calling himself a programmer and doesn’t understand synchronization issues, or a programmer that couldn’t understand C pointers means that he is not a programmer. Programming is a talent there are people who has talent and write great programs, and the other 95% are just people who call themselves programmers but they are far away from programming, and this is why we always have to adjust crappy code done buy those 95%.

    Omar.

  6. يقول admin:

    بالنسبة لحزمة AAnimation التي كلمتك عنها
    هذا رابط لها
    يمكنك الإطلاع عليها

    بأمانة أنا لم أعطي المستخدم الصلاحية المباشرة على AnimationList
    وذلك لأني أحتاج إلى القيام بعملية synchronization الخاصة بي
    التي لا علاقة لها باستخدام synchronization الموجودة في الجافا
    على كل أنا أخبرتك أني مؤيد للفكرة من قبل

    بالنسبة لموضوع الأخ vector قلة ما أستخدمه
    لكني دائماً ما أنصح به بالنسبة للملتحقين الجدد بدروس multithreading
    وذلك إيماناً مني بالتدرجية
    بالعكس مع المحترفين الذين من المفترض بهم استخدام wrapper pattern

    على كل يبقى الموضوع موضوع trade off
    هل تهتم بسرعة البرنامج أم بسرعة إنتاجه
    أظن أنك توافقني أن ضبط عمليات التزامن سيأخذ وقتاً محترماً من وقت العمل

    سؤال على الهامش: لماذا تكتب باللغة الإنجليزية؟

  7. يقول Omar Al Kababji:

    Yes its always a trade of, I write in english because I think is more universal than the arabic language, so everybody can read not only arabic readers, plus I prefer the english terms for programming such as (multithreading, synchronization, … etc) they are more clear than their arabic counterparts (which I honestly read for the first time on your blog). and finally because I know by memory the english keyboard layout and don’t have the arabic one (yes I know can add it).

    In other words I prefer english because its more universal.

  8. يقول admin:

    سررت بناقشي معك وأتمنى لو تزورني من فترة للأخرى
    بالنسبة لموضوع المصطلحات أتعمد كتابة المصطلح الإنجليزي بجانب ترجمتي
    لكي لا يصعب على من يعرف المصطلح الإنجليزي فهم مقصدي
    على كل أعتقد أن الكتابة باللغة العربية تعطيني آفاقاً أكبر في الكتابة
    وتسمح لي بالتعبير بشكل أدق

    تحياتي

Leave a Reply