Files
HW-09-Advanced-Multithreading/answers.md
T

28 KiB
Raw History

> 463 0 bjbjww .^k^kvvooooo  l#vvv#######X%(H#ovvvvv#oo&# vRoo# v# ""D(""<#0l#"V( V("V(o",vv vvvvv##vvvl#vvvvV(vvvvvvvvvv 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.

  • AtomicInteger
  • AtomicLong
  • AtomicBoolean
  • AtomicReference

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. /01QRS      # $ 5 6 I J K _ a      3 4 5 h{hqZ)01RS    $ 6 J K a    4 5 gdqZ,1h/ =!"#% "#x666666666vvvvvvvvv666666>6666666666666666666666666666666666666666666666666hH66666666666666666666666666666666666666666666666666666666666666666p62&6FVfv2(&6FVfv&6FVfv&6FVfv&6FVfv&6FVfv&6FVfv8XV~ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@ 0@66666OJPJQJ^J_HmH nH sH tH NN Normal dCJKH_HaJmH sH tH bb { Heading 1$$hP@&!B*CJ(OJPJQJ^JaJ(phGabb { Heading 2$$P@&!B*CJ OJPJQJ^JaJ phGaZZ { Heading 3$$P@&B*CJPJ^JaJphGaXX { Heading 4$$P(@&6B*PJ]^JphGaRR { Heading 5$$P(@&B*PJ^JphGaXX { Heading 6$$(@&6B*PJ]^JphYYYRR { Heading 7$$(@&B*PJ^JphYYYTT { Heading 8$$@&6B*PJ]^Jph'''N N { Heading 9 $$@&B*PJ^Jph'''DA D Default Paragraph FontRiR 0 Table Normal4 l4a (k ( 0No List R/R {Heading 1 CharCJ(OJPJQJ^JaJ(phGaR/R {Heading 2 CharCJ OJPJQJ^JaJ phGaJ/J {Heading 3 CharCJPJ^JaJphGaH/!H {Heading 4 Char6PJ]^JphGaB/1B {Heading 5 CharPJ^JphGaH/AH {Heading 6 Char6PJ]^JphYYYB/QB {Heading 7 CharPJ^JphYYYH/aH {Heading 8 Char6PJ]^Jph'''B/qB  {Heading 9 CharPJ^Jph'''T>T {TitledPm$ @CJ8KHOJPJQJ^JaJ8L/L { Title Char @CJ8KHOJPJQJ^JaJ8RJR {Subtitle  & F@B*CJPJ^JaJphYYYL/L { Subtitle Char@CJPJ^JaJphYYY@@ {Quote $a$6B*]ph@@@8/8 { Quote Char 6]ph@@@DD { List Paragraph ^m$H!H {PIntense Emphasis6B*]phGa !{ Intense QuoteB $hh$d &d NGa PGa ]^a$6B*]phGaH/H {Intense Quote Char 6]phGaP!!P {Intense Reference5:@B*\phGaPK![Content_Types].xmlN0EH-J@%ǎǢ|ș$زULTB l,3;rØJB+$G]7O٭V<a(7IR{pgL=r85v&uQ뉑8CX=$␴?6NJCFB.ʹ'.+YT^e55 ð _g -;Ylݎ|6^N?[PK!֧6 _rels/.relsj0 }Q%v/C/}(h"O = C?hv=Ʌ%[xp{۵_Pѣ<1H0ORBdJE4b$q_6LR70̞O,En7Lib/SeеPK!kytheme/theme/themeManager.xml M @}w7c(EbˮCAǠҟ7՛K Y, e.|,H,lxɴIsQ}#Ր ֵ+!,^$j=GW)E+& 8PK!wG theme/theme/theme1.xmlYKo7X콱$۲eD&~RRHi]傤lV$^ HA/1\H0}R ? gF48\5J"Ig0e,pEO  )9уe$g]Ix\)M\RϥdaR(<fRm;WB  i0HHV8zBTΐ!WD5'妕VmyZ*qeI [gu^YX d>zt7.??Źڨd/L+[ ¯UjyA/֖j-A %4/%ި+K|h(Kmgk):b| H$Y 9DZ.6)A2&RV!*_[dI+^DL-)>qfoΟ~'ҨrQrW_ƿw!pƦ7/zbp@m ?ItDl,(CjLQc׎O9!N@Iw{Xe3b|:2˭A9Tn$ءy@Q&Q3,;sg8v%= CN4I ~> zHzw0u $J}*;(wL|$C޳q-!1,hEX>ZN yݾKt l&;HPm%6sq !&}] ~@Lw?%q d98@7mia_Y㩓b8F vB{cNQqsu;6~@VƾzXU+f:OllD,E|=mVePاcGxe_+gj=HSԳ;{yи½|mHmA0DcU\+w/ 9MOJK:WIUsffn<"K*2:&w4]3۸cB/br;}̸uF̘G}ҙ3>-=k&ڂE% |Te6pp1GZ&L~AdNPjĢP gFFz٫[ euRTVxC'c*ibQTz:"dC%1!4ZŲR?rՔ)Zf\5GμMO20V4'|:xt&Ԯiv +nD?TWq]_7.u)S 4?@3;S,8;^K=7cC ^$77&%Bn"cy@I K?L'CWc%WSc#^w߭ei)$ Z%v]:Blqwae&De*2JǐYG4OPQRlnຠtSi832IQ QrZ.afٽ\HYʚU6iaT&lyo]M̹Qh2/woQoPSJŪ[<FU#v+w;XQɨԖ/ڬ{c5oiO**E~o,0\5 ICHt kѼm݀F+1UJAo x%j.l)l s)]RB w)'>lDY&Riڊ0ܫ/PK! ѐ'theme/theme/_rels/themeManager.xml.relsM 0wooӺ&݈Э5 6?$Q ,.aic21h:qm@RN;do7gK(M&$R(.1r'JЊT8V"Hu}|$b{P8g/]QAsم(#L[PK-![Content_Types].xmlPK-!֧6 0_rels/.relsPK-!kytheme/theme/themeManager.xmlPK-!wG 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  ?"'49Hbo33\4B؄^CJOJQJo(^CJOJQJo(opp^pCJOJQJo(@ @ ^@ CJOJQJo(^CJOJQJo(^CJOJQJo(^CJOJQJo(^CJOJQJo(PP^PCJOJQJo(\4BqZ*6{mH@@UnknownG.[x Times New Roman5Symbol3& .[x Arial3. AptosC. Aptos Display?5 .Cx Courier New;WingdingsA$BCambria Math"qh; ; q0KQ@P $P{2!xxk FatemehAngelo . Oh+'0t  4 @ LXhpxFatemeh Normal.dotm Angelo .5Microsoft Office Word@G@4@L+;GVT$mi i    0."System- "System-"System--@"Aptos--- (2 o0 # Java Concurrency        2 o0   2 o0   22 o0 Atomic Variables and Locks        2 o0     2 0    ;2 0 ## 1. What are atomic variables?          2 B0     2 0    _2 80 Atomic variables are special variables that allow thread                2 0 - 82 0 safe operations without using           2 U0 locks. They ensure that operations such as reading, writing, or updating a value are                         2 !W0 performed as a single, indivisible action. Unlike ordinary variables, atomic variables                      2 8T0 prevent race conditions when multiple threads access the same data at the same time.                     2 80     2 Y0    2 {0 ---  2 {o0     2 0    2 P0 ## 2. Name at least four classes from the java.util.concurrent.atomic package.                       2 0     2 0     2 0 -  2 e0   "2 i0 AtomicInteger      2 0     2 !0 -  2 !e0   2 !i 0 AtomicLong      2 !0     2 B0 -  2 Be0   "2 Bi0 AtomicBoolean      2 B0     2 d0 -  2 de0   %2 di0 AtomicReference       2 d0     2 0    )2 0 ### Typical use case       2 0     2 0    M2 ,0 AtomicInteger is commonly used as a thread              2 0 - A2 $0 safe counter. For example, multiple          2 P0 threads can safely increment a shared counter without causing incorrect results.                   2 0     2 !0    2 B0 ---  2 Bo0     2 c0    J2 0 ## 3. Compare locks with atomic variables.             2 0     2 0   "Arial--  00//.. ՜.+,D՜.+,t    (08@ H U   Title ?GKSOProductBuildVer2052-9.1.0.3914  !"#$%&'(),-./0125Root Entry FpXD71Table f(WordDocument.SummaryInformation(DocumentSummaryInformation8+CompObjr  F Microsoft Word 97-2003 Document MSWordDocWord.Document.89q