28 KiB
��ࡱ�����������������>���� ���������������4����������6��������������3�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ����������0�����������bjbj�w�w������������������ �.���^k�^k������������������������������������������������������������������������������v������v��o������o������o������o������o�������������������������������������������������������������l#����������������������������������������v������v������v������#�����#������#������#������#������#������#������X%�����(��H���#���������������������o������v����������������������v������v������v������v������#��������������o������o����������������������������&#����� ������ ������ ������v��R���o�������������o�������������#�������������� ������������������������������������������������������v������#�������������� ������ ������"������������������������������������������������������������������������������"�����������������������D���������������������(����"��������������"�����<#��0���l#�������"�����V(��������� ���V(������"������������������������������������������������������������������������������V(��������������o�������"��,���v������v������ ������v������v��������������������������������������v������v������v������#������#����������������������������������������������������������������������������v������v������v������l#������v������v������v������v��������������������������������������������������������������������������������������������������������������������������������������������������������������V(������v������v������v������v������v������v��������������������������������������������������������������v������v������v������v�����5��:����� ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������# Java Concurrency � Atomic Variables and Locks
1. What are atomic variables?
Atomic variables are special variables that allow thread-safe operations without using locks. They ensure that operations such as reading, writing, or updating a value are performed as a single, indivisible action. Unlike ordinary variables, atomic variables prevent race conditions when multiple threads access the same data at the same time.
2. Name at least four classes from the java.util.concurrent.atomic package.
AtomicIntegerAtomicLongAtomicBooleanAtomicReference
Typical use case
AtomicInteger is commonly used as a thread-safe counter. For example, multiple threads can safely increment a shared counter without causing incorrect results.
3. Compare locks with atomic variables.
Atomic variables are a good choice for simple operations, such as incrementing a counter or updating a single value. They are usually faster because they do not block other threads.
Locks are a better choice when multiple operations must be performed together as one atomic task or when working with several shared variables that must remain consistent. Although locks have more overhead, they provide greater flexibility for complex synchronization.
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������/��0��1��Q��R��S��� ��� ��� ��� ��� ��� ��
��
��
��
��
��#
��$
��5
��6
��I
��J
��K
��_
�� ��a ������������ ��3��4��5���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������h�{���hqZ�)���0��1��R��S��� ��� ��� ��� �� �� �� ��$ ��6 ��J ��K ��
��a
�������� ��4��5����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������gdqZ��,�1�h��/ ��=!��"��#����%�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������"#���x����������������������������������������������6��6��6��6��6��6��6��6��6��v��v��v��v��v��v��v��v��v��6��6��6��6��6��6��>��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6������6��6�����6��6��6��6��6��6��6��6������6��6��6��6��6��6��6��6��6��6��6��6��h��H��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��6��p��6��2���������������������&��6��F��V��f��v������������������������2��(��������&��6��F��V��f��v������������������������&��6��F��V��f��v������������������������&��6��F��V��f��v������������������������&��6��F��V��f��v������������������������&��6��F��V��f��v������������������������&��6��F��V��f��v��������8��X���������V��~������������������������������� ��0��@������������� ��0��@������������� ��0��@������������� ��0��@������������� ��0��@������������� ��0��@������������� ��0��@������������� ��0��@������������� ��0��@������������� ��0��@������������� ��0��@������������� ��0��@������������� ��0��@������������� ��0��@��6��6��6��6��6�����OJ�PJ�QJ�^J�_HmH nH sH tH ����N�����N����������N�o�r�m�a�l������d�����CJ�KH�_HaJ�mH sH tH b����b���{���� �H�e�a�d�i�n�g� �1�����$$�h�P�@&��!�B*�CJ(�OJ�PJ��QJ�^J��aJ(�phGa��b����b���{���� �H�e�a�d�i�n�g� �2�����$$����P�@&�!�B*�CJ �OJ�PJ��QJ�^J��aJ �phGa��Z����Z���{���� �H�e�a�d�i�n�g� �3�����$$����P�@&��B*�CJ�PJ��^J��aJ�phGa��X����X���{���� �H�e�a�d�i�n�g� �4�����$$�P��(�@&��6�B*�PJ��]�^J��phGa��R����R���{���� �H�e�a�d�i�n�g� �5�����$$�P��(�@&��B*�PJ��^J��phGa��X����X���{���� �H�e�a�d�i�n�g� �6�����$$�(����@&��6�B*�PJ��]�^J��phYYY��R����R���{���� �H�e�a�d�i�n�g� �7�����$$�(����@&��B*�PJ��^J��phYYY��T����T���{���� �H�e�a�d�i�n�g� �8�����$$���@&��6�B*�PJ��]�^J��ph'''��N� ���N���{���� �H�e�a�d�i�n�g� �9���� �$$���@&��B*�PJ��^J��ph'''��D�A ����D���������D�e�f�a�u�l�t� �P�a�r�a�g�r�a�p�h� �F�o�n�t�����R�i�����R� ������0�T�a�b�l�e� �N�o�r�m�a�l�������4�� l�4����a�������(�k ����(�� ������0�N�o� �L�i�s�t���������R��/����R����{�����H�e�a�d�i�n�g� �1� �C�h�a�r����CJ(�OJ�PJ��QJ�^J��aJ(�phGa�R��/��R���{�����H�e�a�d�i�n�g� �2� �C�h�a�r����CJ �OJ�PJ��QJ�^J��aJ �phGa�J��/��J���{�����H�e�a�d�i�n�g� �3� �C�h�a�r����CJ�PJ��^J��aJ�phGa�H��/��!H���{�����H�e�a�d�i�n�g� �4� �C�h�a�r����6�PJ��]�^J��phGa�B��/��1B���{�����H�e�a�d�i�n�g� �5� �C�h�a�r����PJ��^J��phGa�H��/��AH���{�����H�e�a�d�i�n�g� �6� �C�h�a�r����6�PJ��]�^J��phYYY�B��/��QB���{�����H�e�a�d�i�n�g� �7� �C�h�a�r����PJ��^J��phYYY�H��/��aH���{�����H�e�a�d�i�n�g� �8� �C�h�a�r����6�PJ��]�^J��ph'''�B��/��qB� ��{�����H�e�a�d�i�n�g� �9� �C�h�a�r����PJ��^J��ph'''�T�>���T���{�����T�i�t�l�e�����d����P�m$� �@���CJ8�KH�OJ�PJ��QJ�^J��aJ8�L��/���L����{���� �T�i�t�l�e� �C�h�a�r��� �@���CJ8�KH�OJ�PJ��QJ�^J��aJ8�R�J���R���{�����S�u�b�t�i�t�l�e��� �� &F����@��B*�CJ�PJ��^J��aJ�phYYY��L��/���L����{���� �S�u�b�t�i�t�l�e� �C�h�a�r����@��CJ�PJ��^J��aJ�phYYY�@�����@���{����Q�u�o�t�e�����$���a$�6�B*�]�ph@@@��8��/���8����{��� �Q�u�o�t�e� �C�h�a�r����6�]�ph@@@�D�����D����{�� �L�i�s�t� �P�a�r�a�g�r�a�p�h��� ����^��m$���H�!���H����{��P�I�n�t�e�n�s�e� �E�m�p�h�a�s�i�s����6�B*�]�phGa����������!��{��� �I�n�t�e�n�s�e� �Q�u�o�t�e���B� �$�h�h$d� &d� N�Ga� �P�Ga� �]^a$�6�B*�]�phGa��H��/��H�� ��{����I�n�t�e�n�s�e� �Q�u�o�t�e� �C�h�a�r����6�]�phGa�P�!��!P����{����I�n�t�e�n�s�e� �R�e�f�e�r�e�n�c�e����5�:�@��B*�\�phGa�PK�����!�������������[Content_Types].xml���N�0E�H���-J��@%��ǎǢ|�ș$�ز�U��L�TB� l,�3��;�r��Ø��J��B+$�G]��7O٭V��<a������(7��I��R�{�pgL�=��r����8�5v&����uQ�뉑8��C����X=����$�?6N�JC��������F�B.ʹ'�.�+���Y�T���^e5�5�� ��ð�_�g -��;�����Yl�ݎ��|6^�N���?���[�����PK�����!��֧�����6�����_rels/.rels���j�0���}Q��%v/��C/�}��(h"���O�
������=������� ����C?�h�v=��Ʌ��%[xp��{۵_�Pѣ<�1�H�0���O�R�Bd���JE�4b$��q_����6L��R�7�������0̞O��,�En7�Li�b��/�S���e��е���������PK�����!�ky������������theme/theme/themeManager.xml�M � @�}�w��7c�(Eb�ˮ���C�AǠҟ�����7��՛K Y,� �e�.���|,���H�,l����xɴ��I�sQ}#Ր���� ֵ+�!�,�^�$j=�GW���)�E�+& 8������PK�����!��w�G��� �����theme/theme/theme1.xml�YKo7��X콱$۲eD���&~�RR�Hi�]��傤l�V$�^ H������A/�1�����\�H��0���}�R�?�gF����48�\�5���J���"����Ig���0e�,��p�E��O���� ���)���9уe$��g�]��Ix��\��)�M�\�R�ϥ�da����������R����(<fR���m�;WB��� �����i��0�HH��V8�z�BTΐ!WD�5�'��妕Vmy�Z���*�q�e������I
[gu�^Y�Xd>zt7���.��??Źڨ���d�/L�+[��梃� �¯Uj��y�A��/�֖j-�A %��4����/�%����ި�+K�|��h(�Km�g��k):b|�
H�$Y �9��D�Z.�6��)�A�2&�R�V!�*��_[dI+^�DL-)>��q��f���������oΟ�~�'Ҩr��Q�r����W_���������ƿ���w��!�p�Ʀ�����7�/z��qԵ��b������p@m
�?���ItDl��,(Cj���L��Q���cO9�����!�N�@���I��w���{��X�e��3�b��|�:����2�˭A9��Tn$ءy@Q&Q�3,�;��s�g�8v�%=���� ����C�N4���I ~�����> ���z��H��z�w0u�� $J}*;(���w�L|$�Cq-!��1�,hEX��>��ZN� �yݾK������t� �l�&;�HP���m�%6�sq!��&}�]���~@�Lw?%�q���� d9��8@�7���C̜�mia_�Y㩓b�8�F�� vB{c�NQ�q��s�u�;6�~�@V�ƾ�z��XU��+��f:O���l�l���D��,E|��=��m�V��e�Pا�c�G��x�e_�+�gj=H�S�Գ����;����{y�и½|mH���m�A0D����cU\���+�w/��
�9MOJ�K:���������W���nǯ�IU��sf�����fn<"K����*2���:���&��w4���]3�۸�cB�/���b�r;}̸u��F�̘G}ҙ3�>��-��=��k&ڂE%�睸�� |Te6pp1GZ&�L~Ad�NP��j��ĢP� gFFz٫[�� �e�u��R�TV��x��C'�c*i���bQ���T����z�:"�d�C���%1�!�4Z���������ŲR?rՔ)�Z������f�����\��5�����G��μMO�2�0V4'��|����:�x�t&Ԯ�i��v� +����n�D?���T�W�q]_7�.u�)S�� ��4��?�⦾���@3;S�,8�;^�K=�7�>̌�c�C����^��$77�&�%�Bn"����c���y@I���K?�L'C�W�c%WS�c#^w���}ܓ�߭ei�)�$��Z%���v�]:��Blq��������wae&��De*���2JǐYG4OPQR�lnຠ�t�Si��83�2IQ �����Q�rZ�.�afٽ\HY�ʚ����U6�i��aT&lyo����]�M��̹�Q��h�2/�w��oQo�PS����J�Ū[<F���U�����#�v+��w;X�Q��ɨ�����Ԗ�/���ڬ{�c��5o�iO**E~��o�,�0���\5�
I�C�Ht�k��Ѽm�݀F+1U�JAo��
x%j.l)l�s)]�RB��w)�'�>��lDY����&��R�iڊ0�z۶���ܫ���/������PK�����!� ѐ�������'���theme/theme/_rels/themeManager.xml.rels��M �0���wooӺ�&݈Э���5 6?$Q�� �,.�a��i����c2�1h�:��q��m���@RN��;d��o7�g�K(M&$R(.1�r'J��ЊT���8��V�"��AȻ�H�u}���|�$�b{���P����8�g/]�QAsم(����#��L�[����������PK-������!���������������������������[Content_Types].xmlPK-������!��֧�����6���������������0��_rels/.relsPK-������!�ky������������������������theme/theme/themeManager.xmlPK-������!��w�G��� ������������������theme/theme/theme1.xmlPK-������!�
ѐ�������'�������������Q
��theme/theme/_rels/themeManager.xml.relsPK������]��L����<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a:clrMap xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" bg1="lt1" tx1="dk1" bg2="lt2" tx2="dk2" accent1="accent1" accent2="accent2" accent3="accent3" accent4="accent4" accent5="accent5" accent6="accent6" hlink="hlink" folHlink="folHlink"/>�������!�������������������������������8��������������������������@���������������������������������������0�����(���� ���������������������
������������B����
���������S������������������ ���?���������������������������"��'��4��9��H��b��o�����������������������������������������3��3������������������������\4B���������������������������������������������^�����CJ�OJ�QJ�o(������������������������������������^�����CJ�OJ�QJ�o(��o��������������������������p�����p^�p���CJ�OJ�QJ�o(�����������������������������@�����@^�@���CJ�OJ�QJ�o(����������������������������������^����CJ�OJ�QJ�o(������������������������������������^�����CJ�OJ�QJ�o(������������������������������������^�����CJ�OJ�QJ�o(������������������������������������^�����CJ�OJ�QJ�o(�����������������������������P�����P^�P���CJ�OJ�QJ�o(�������\4B��������������������������������������������������qZ�*6���{���m���H��������������������@������������@��������U�n�k�n�o�w�n�����������������������������������������������G����.��[x�� ��������������T�i�m�e�s� �N�e�w� �R�o�m�a�n���5�������������������������S�y�m�b�o�l���3&����.��[x�� ��������������A�r�i�a�l���3.��������������� ������������������A�p�t�o�s���C.��������������� ������������������A�p�t�o�s� �D�i�s�p�l�a�y���?5��� �.��Cx�� ��������������C�o�u�r�i�e�r� �N�e�w���;����������������������������������W�i�n�g�d�i�n�g�s���A�������$�B��������������C�a�m�b�r�i�a� �M�a�t�h���"��q������h������秦���������������;�������� �������;������ �������q����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������0�����������������������������������������������������������������������������������������K�Q�������������������������������@P���� ���$P���������������������������{�����2���������������������!�����������������������������x���x�����������������k��������������������������������F�a�t�e�m�e�h��A�n�g�e�l�o� �.��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ��������������������������Oh���+'��0���t������������������������������������������������������������� ���������� ���4�����@�� ���L�����X����������h�����p�����x����������������������������������Fatemeh���������������������������Normal.dotm�������Angelo .����������5���������Microsoft Office Word���@������G����@����4����@����L�+������������������;���������G��������������VT$mi� ��i
����������������������� 0���.�����������������"System�����������������������������-������� ����������"System�����������������������������-�����������������������"System�����������������������������-�����-������������������������@�"Aptos������������������������������-�����-�����-��������� ���(���2
o��������0 # Java Concurrency � ��� �� ��� � �� ���� ������� ��� ���2 o���������0 ������ ��� ���2 o���������0 ����� ���2���2 o���������0 Atomic Variables and Locks �� ������ ��� � ����� � � ��� ������� ��� ���2 o��������0 � ���� ���������� ��� ���2 ���������0 � ���� ���������� ���;���2
��� ������0 ## 1. What are atomic variables? � �� ���� � ��� ���� �� ������ ��� � �������� ��� ���2 ��B������0 � ���� ���������� ��� ���2 ���������0 � ���� ���������� ���_���2
���8������0 Atomic variables are special variables that allow thread �� ������ ��� � ����� ����� ���� ���� ��� � ������ � ��� ��� ���� ��� � ���� ��� ���2 ���������0 -����� ���8���2 ���������0 safe operations without using � ���� � ��� ��� � ������ � � ��� ��� ������ ���������� �������2 �U������0 locks. They ensure that operations such as reading, writing, or updating a value are �� ������� ����� �� ����� � ��� � ��� ��� � ���� �� �� ����� � �� ��������� ���� ��� � � � ��� ��� ��� �� ��� ������� ���������� �������2
!�W������0 performed as a single, indivisible action. Unlike ordinary variables, atomic variables � ���� ���� �� ��� ���� ������� � ������ ���� ���� � ���� ������ �� �� � ����� ��� � ������ �� ������ ��� � �������� ���������� �������2 8�T������0 prevent race conditions when multiple threads access the same data at the same time. ����� ���� ����� � � ���� � ���� �� ��� ���� ����� ��� � ��� �������� ���� ���� � �� �� ���� ���� ������������ ���
���2
8�������0 � ���� ���������� ���
���2
Y�������0 � ���� ���������� ������2 {�������0 ---������� ���
���2
{o�������0 � ���� ���������� ���
���2
��������0 � ���� ���������� �������2 ��P������0 ## 2. Name at least four classes from the java.util.concurrent.atomic package. � �� ���� ���� ����� ����� � ����� �������� ���� ��� �� �� �� ������ � �� ���� ��� �� ���� �� � ��� ������� ���
���2
��������0 � ���� ���������� ���
���2
��������0 � ���� ���������� ��� ���2 ��������0 -����� ���
���2
�e�������0 ����� ���"���2
�i�������0 AtomicInteger� � �� ����� ������ ���� ���
���2
���������0 � ���� ���������� ���
���2
!�������0 -����� ��� ���2 !e�������0 ����� ������2 !i�������0 AtomicLong � �� ����� � �� ���� ��� ���2 !��������0 � ���� ���������� ��� ���2 B�������0 -����� ���
���2
Be�������0 ����� ���"���2
Bi�������0 AtomicBoolean� � �� ����
� � ��� � � ���� ���
���2
B��������0 � ���� ���������� ���
���2
d�������0 -����� ��� ���2 de�������0 ����� ���%���2 di�������0 AtomicReference� � �� ���� ������ ��� ���� ��� ���2 d��������0 � ���� ���������� ��� ���2 ��������0 � ���� ���������� ���)���2
��������0 ### Typical use case � � ���� ��� ��� ����� ������ ��� ���2 ���������0 � ���� ���������� ��� ���2 ��������0 � ���� ���������� ���M���2
��,������0 AtomicInteger is commonly used as a thread � �� ����� ������ ������ ��� � ���� ��� �� ��� ��� ��� � ���� ��� ���2 ��������0 -����� ���A���2 ��$������0 safe counter. For example, multiple � ����� � � ������� ����� �� ������ ���� ������� ���������� �������2 ��P������0 threads can safely increment a shared counter without causing incorrect results.� ��� � ���� � ��� ������� ������ ��� ��� � ��� ��� � � �������� � � ���� � ��� ���� �� ���������� �������� ���
���2
��������0 � ���� ���������� ���
���2
!�������0 � ���� ���������� ������2 B�������0 ---������� ���
���2
Bo�������0 � ���� ���������� ���
���2
c�������0 � ���� ���������� ���J���2 ��������0 ## 3. Compare locks with atomic variables. � �� ���� �� � ����� �������� �� �� ������ ��� � �������� ���
���2
��������0 � ���� ���������� ���
���2
��������0 � ���� ������������������"Arial������������������������������-����-����������� ����� 0�����0�������������������/����/�����������������.����.�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� �����������������������՜.���+,��D�����՜.���+,�����t�������������������������������������������������������������������������� ����� ������������� �����(�����0�����8�����@�� ���H�����U���������������������������������������������������������� �������������������������������������������������������������������������������������������������Title������������������ ������?������G������������KSOProductBuildVer�������������2052-9.1.0.3914���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� �������������
���������������������������������������������������������� ���!���"���#���$���%���&���'���(���)����������,���-���.���/���0���1���2�����������5���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������R�o�o�t� �E�n�t�r�y��������������������������������������������������������� ������������F������������pX�D��7�����������1�T�a�b�l�e�����������������������������������������������������������������������������������������������������
���f(������W�o�r�d�D�o�c�u�m�e�n�t���������������������������������������������������������������������������������������������.�������S�u�m�m�a�r�y�I�n�f�o�r�m�a�t�i�o�n���������������������������(����������������������������������������������������������D�o�c�u�m�e�n�t�S�u�m�m�a�r�y�I�n�f�o�r�m�a�t�i�o�n�����������8�������������������������������������������������+�����������C�o�m�p�O�b�j�������������������������������������������������������������������������������������������������������r���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������ ������������F ���Microsoft Word 97-2003 Document�
���MSWordDoc����Word.Document.8��9�q��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������