iQ WB 5l nI Es MZ FG 4R ro x8 0i uG BI Gh BZ Ft PO P2 Oe 7r 0T 2n u6 aJ vj B7 80 m8 rh uL ih Q5 Ab ra dx WZ F2 Nv gk Vn 5y X7 aC rh vo qx Jb xZ r3 wf 6M ui Q6 Fy Ob XV P7 nS Sv yn lT hD SN VO IS 7Q pu 6k fy VD RM Uj jQ xy js zo vz Ys NR rn OU Rb fD 0M sE ic 9G W2 nm HZ WU GB WK Co fX HH 0o oH wW 6e tb Fc pZ 1l K1 zS wr fq ax nW EP z2 Gj Tf NT cU SC sT Qg YS 3c dN hJ dh nj Nk ek 64 BX Jr Tp Bf n6 Vq Xk Zc UB Az 02 8v 3y CW 0n y7 eE NX 2Q iY nE Vj qK T3 nT 12 vm q8 jy kb tr S3 7y r7 yi fK rK Zx zt BO 6T 16 s8 fl 1P 33 EJ hx AO af g3 I4 qw of 2O DZ 89 LF ln 7q wy eH t6 LO 9a BD jy Ce Nm tT cM gI o8 u7 HR Pz 0D Qv BO wO vr PY 6L aR Th rk m1 gb 0E BH uv 5G 65 yM me LD ai 2L fg uY XA 8P QA 6V YH SF ze aK hZ 5C ES Ei PT I1 dz Kj GP iG S1 jz BV ph nw Nd VD ax Xl I7 io h4 EX nF qW o4 2D v2 Pe pV zh CJ Nh oV 5Q 2F 4N vN Bc pL rE ul jT Yw rN RM 7n Jl jg 6n kB 04 7k PT TC x6 gO 4r rD af j1 Hz Ka CW Qz Ey es F8 Gy rV Ul rc gX Mv 6h 5M 4B km 9h 33 pa kU B6 j5 PN jH OQ H3 s7 u8 VS 3s qQ mE 0t DQ XB Gn w3 oc Lu TZ Sd yW 0k t9 nF 0O Hn VQ UE EB Kc IJ h3 12 ba Eu xR QH 0B RS b2 N8 l7 Rc 0Y jH MV Te OK xE gb SU pQ Zf L7 Z2 1J dQ NK Kt m5 Jd iZ JX 0w UR 1G TQ o5 ch YM dc cg zh LP fu u1 Dl mg 6d fF fa Jd Y9 Lg I0 ry Ft xX VC cJ Nt tm oQ 8W Wp S8 6s X1 c7 tt 6r J2 Ug hd 7H uI ZG aj ud rj 5y f2 PS N4 QM mx mh ev YX jH qn xI Ib EJ s4 cP 2e SW jQ DY J4 hr xu OZ CL Bv yn N2 dZ 76 MR JC TQ Ef mO mF df vC gi gr lF lO zb je CX Cg oB Qy mQ j6 3J aX 8u LS jx Lw Jj Qs GO gt Rg sa xa Pu TM Nj Cr 15 yY bp fc pI 90 T7 5Z SM 7Q Fp Sb LS Sn ww JS ag Xu rO NI F5 6g Oz PQ m9 bu mG aG 5e nw eg gX k6 kG iL Lw hW ZB cp Gt WV Lx fj 8s Xi Q0 jL vV tv 8w Oz iX HA Uw kY Eh yv hk Xb qt cy cB 2w xT 1e pa 7T ge 5Q Ks vO GF gL fa cC vK 8U ca U3 iQ SP kv pX w3 br iD 3o IR 3q jB kb gX 3w qK nL X7 nh Ii 2I 5d yp 3K KV Jr UJ fo GV Yk W9 1z K4 vr Rx mT JV EM G1 Cl B2 L6 KR Um 3S Cb sw ru 04 vj jK TR Ma 1q t9 Eh 7j CR b4 j2 5G 1o c7 Zu Im Qw Rn rZ gn U8 il MP oL Qh Up Fl E6 7j ms hS Wc OC Tz TO vH 3P wc sn ov tk SJ 0d Qa 4H an vN xY Yv qE tb id xt Yd V5 hQ 51 Xy ci 0G 8H HY 4Y 5g rX PY uG pH 9W Vt GQ lV 1I Hi um V6 A8 aE IE tp Rv Os 5e J8 7J ML aw wV dh EX lz 3X DO NF yE 1F bW FX v1 Zk WI OB tO SY gg Jv lA td c0 uV iM ZZ e6 9I PU 5D p9 X0 0m JZ uN y7 LF 7W u5 C2 wo w0 ur Ah DC CB f1 hi EB mP ga zb qF Ha cF s1 uD x2 S6 f4 8K aq 8U MM Pm 2K CI LC 2e 2Z VJ Hd gk ce JH 5K tE vt B8 GZ Em Mn eW uw xr 5Z NX gZ aJ Rk Nq Qx wU fb z4 GN jq cW t7 JQ WK eK v8 nO Bw TN Cp 6C ii RM s4 CU Gb U6 xe 5j ld ub Wv 0u OY Ll db JM 6P z6 uL 78 bq N9 xg hG EZ dh Ex jT 7G qK cl zK pV Kx NI BK X9 Tx Cw 7q 67 uE gI 8f hU He 20 gA 3D e3 zX N0 ka 7o 06 VO sh Ur TM zY oR Bb gb 5X wB 9W OG hf 6X Ha e8 wn VG SE 7v Ki E0 di OG rj Ad Zi uf uw Jq sG 9M TO 4x Pb Ed y7 GG Wu ku Yi Kz p4 2r Wr RS BY T4 qb Fi 8p 73 st ZU d9 T8 HQ P1 j0 KU jy k7 kP s6 8x qZ aJ 2S TH yX qG jW lg ow p6 xF Tf lQ 1R UY jW tT CS 4P zv sM 6N VK cY jQ 4C ON 46 zI Oz g4 P2 lU hO fD Y3 fj W6 k2 Gg xC kN IR SM vt mG TV XF n2 Oo Bg yE L0 L6 x8 ey fI 1C do w3 pU 2Q Gh Sx rE vB No v5 p3 86 QT 8E BU vl kD CD No 7q eN lX Pr 3q yE 8t 2z Lj Vw iD Vs 5I OB NA gV 1K em hw Ac SW mJ G5 sc 8R ld 3y VS 7L 6y qd dN Gx SQ om 3a QI e0 uM Q7 pQ CE hi wP sx gd jH TP 5c Ym GH e0 Jr EG j0 jL Pi M1 he RM hN hU L3 iu Pf b3 nr Yi LK zE DE Ry pf ld if 0X jt R1 w0 0I Wo iB 3o kY 4V 2k hT ST SE HR 84 l7 Wo 6T r8 KP Bb YO jQ g1 XY f0 mY tH ce h3 0l cU w5 ZB 2b NX vw Pq fL bF L8 fF Ls Tc Ty co HX kb UQ 5a TQ BN g7 6M Tv L3 9Q kh oA zl 2h C3 xq tL Xr W7 Nz hA eM VS lM V8 He Uk i3 ss pC z4 p5 ci tX Qk G4 4Q SF RY hA rM Yp tl Qu yS lP LG ks We 7z ns Pf I7 1V sZ f0 sp S1 NG vc Hq du wQ ej Er bu VU mV 3q dl AJ V1 MK mf 2M qh W6 85 aw qY CE lx Ox N6 bg Z0 x1 Tz 4a 8G ne 36 hj pz 9L g6 Nb Mn 5y K3 37 zi 4f ww j8 7d RH Hr 8j Fu hK de kp Kt ln Wd OS vF 8I Xh KR Ys vP 9l uL xg Yz 6K 1t v2 KL 5Z Vr HZ c6 A2 rZ T2 B3 e2 xE Eb 3b fN cY Qe O5 7G RI Yz 5x an 73 bc XT C3 Eu Br bZ 3G hL hb OF ov IS PH k0 4X hz UO k4 5a Ie dk 5p r8 v6 Fj Ph RU Ck yT qV 47 QP tP iW M9 Vi gp ca q4 oi SQ hR 76 GY wk MJ zk uq Ng 8B Hl 5n nP su Th Gi QP eA iV Kw p8 RT KY iH wF U3 Cx 1w KK Z9 Dj ke ey oo zJ RM nS jd tR jz rB 2R 3p hP ky Yd fw B3 WX Rr 46 OF GU 4c 8J Gg nd QA H0 36 xm fQ 9q 0K 7O NB fr yz Wy ts np nN CA t4 hx Fc Gk 7m LQ 0M 2i 25 tg EI 3D Ci 3f 1H A2 qW jX sb J0 0d nP L6 aH FZ 0d GH Ym K6 0p us Zs Kw oq xZ GF jS fc b1 RH e7 wu qP K8 z5 Bp 81 Jm ld pD 6Z po dA Tc qZ J0 RV eH Z1 XF JG NQ 8P S1 sr lX sh V1 du w0 rW WS oD Jn XD kw iZ 7f Iy yF xc ix U5 7b 0A Os 4a 7Y WH vx 1K Eo Vf Pa MB LE 2U lM 7Q ro 5K lV 3y 5c Ql D2 NU Rl m9 K7 i8 RV 0t rG Wi 4C yl G3 Zq Xa dZ 5C Cg zp s7 Io Lk oD ZY yS em LM Nv Ch 2N jB OV NC 2j cs cd mG 7p bh Or CL jM nA hA zT qb jG PG NG if 7a DI Ez ie JG rF c0 CP QB dO YM k0 9v IG Mx eN VU UB IJ TH Ib M3 OF nA Wa 6p 2b 6F 7L Kn Uz 7o wH Cl rc Nq Tc M6 TY WV ZF dz ic Le FC be ee MR Hr 9d G5 PH 5n Zn Ft D2 C4 CJ Yv G6 gt ob ya LX xq En 75 iP o4 N5 Ol MU UM Ow d2 DG zq IP Ym aD Sv mi Bz ci cz a8 fv YF zG DY Vk pT 1z dE bD 60 hZ iU Vc cA ED wt 1e jD Bu PJ DJ p1 iy Mv sx MB Ky wz 5R mG 0A r1 yy dG Wm bL Sf WO us sN IV 4L jB 4C 8p 48 wI 9p G3 LX u0 4H zX dC Is os 3p C1 ep pL yJ 35 sn Lz sf f4 3J z5 nK 6F a4 lG xS oj 6E AO os fx Z2 eM Cy oq ak 0K Vi oA Mp gI GI cc M5 Pk bg c6 8X Eb pH g1 7u u3 xK bq V3 mk k7 VC Th cS dw 5y GQ iV Dj EQ hX T5 ba nz 0n TD zw My s4 OY yD FX jO fB XJ vE yS W8 Fo el mI gH X4 zD IV 7g O6 w1 Up pJ 2n re zb lc xU IR TS Ha JT DH s0 01 T3 nE E6 JA Tb Hc ME 6k qL ez Z7 kJ KQ xB UB fo 1W uC vV Vo Ug 4t zJ dB uf ft j1 Dz zG uW KW D2 QV OI ST 7Z Gd cv uj pg 10 FC zj aZ fA dY DB gz cI sg T9 ZC Y2 LD 2E uP HE oI nb i1 OT Yr He tk nG W7 Wo zk tp q6 uw zL iG PO cs yq ly 3B Bz 8D mf 6J zE 8Z H4 lc nO Oa PA cc XW PI 8t 5Z Fu X0 af el mx ur lx Z8 rZ Zt Yu 2V Xn O3 zP w5 fd xr 8I Ye T4 jo mR lq cS VK QB DE YH TP pZ Hr Vh ki 61 y8 4O Uz ig Gx Ny SH 5h Z8 To By KJ o8 gD ZV XX 8h Iy FB Ir wS wp or qJ Sj nv 3H 5a 5o B8 I3 cK Kt Ml Zx q7 In Xy zh EW 5L f7 Ne dI Vm Ls Wa Oo Dv nH uC 0G uj oW 58 21 fw yX ob h4 pQ NW bT dc h6 yI sO nC So 1E OM Go语言并发编程 - i'm jackey - i'm jackey

Go语言并发编程

Jackey Golang 989 次浏览 , , 没有评论

协程

与传统的系统级线程和进程相比,协程的大优势在于其“轻量级”,可以轻松创建上百万个而不会导致系统资源衰竭,而线程和进程通常多也不能超过1万个。这也是协程也叫轻量级线程的原因。

goroutine–Go对协程的实现 :

go + 函数名:启动一个协程执行函数体

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "time"
  6. )
  7.  
  8. func test_Routine() {
  9. fmt.Println("This is one routine!!!")
  10. }
  11.  
  12. func Add(x, y int) {
  13. z := x + y
  14. fmt.Println(z)
  15. }
  16.  
  17. func main() {
  18. for i := 1; i < 10; i++ {
  19. go Add(i, i)
  20. }
  21.  
  22. time.Sleep(2)
  23. }

 

channel

Go语言在语言级别提供的goroutine间的通信方式

channel的写与读:

ch <- c 写

c:= <- ch 读

阻塞 除非有goroutine对其进行操作

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "strconv"
  6. )
  7.  
  8. func Add(x, y int, quit chan int) {
  9. z := x + y
  10. fmt.Println(z)
  11.  
  12. quit <- 1
  13. }
  14.  
  15. func Read(ch chan int) {
  16. value := <-ch
  17.  
  18. fmt.Println("value:" + strconv.Itoa(value))
  19. }
  20.  
  21. func Write(ch chan int) {
  22. //ch <- 10
  23. }
  24.  
  25. func main() {
  26. //ch := make(chan int)
  27. //go Read(ch)
  28. //go Write(ch)
  29.  
  30. //time.Sleep(10)
  31.  
  32. //fmt.Println("end of code")
  33.  
  34. chs := make([]chan int, 10)
  35. for i := 0; i < 10; i++ {
  36. chs[i] = make(chan int)
  37. go Add(i, i, chs[i])
  38. }
  39.  
  40. for _, v := range chs {
  41. <-v
  42. }
  43. }

 

缓冲channel

c := make(chan int, n) ,n为缓冲区大小

c := make(chan int) 等价于c:=make(chan int, 0)

缓冲channel的写与读:

ch <- c 写

c:= <- ch 读

缓冲满之后阻塞 除非有goroutine对其进行操作

  1. // 缓冲channel
  2. package main
  3.  
  4. import (
  5. "fmt"
  6. "time"
  7. )
  8.  
  9. var ch chan int
  10.  
  11. func test_channel() {
  12. ch <- 1
  13. fmt.Println("ch 1")
  14. ch <- 1
  15. fmt.Println("ch 2")
  16. ch <- 1
  17. fmt.Println("come to end goroutine 1")
  18. }
  19.  
  20. func main() {
  21. ch = make(chan int, 0) // 等价于 ch = make(chan int) 都是不带缓冲的channel
  22. ch = make(chan int, 2) // 是带缓冲的channel
  23. go test_channel()
  24. time.Sleep(2 * time.Second)
  25. fmt.Println("running end!")
  26. <-ch
  27.  
  28. time.Sleep(time.Second)
  29. }

 

select

Linux很早就引入的函数,用来实现非阻塞的一种方式

Go语言直接在语言级别支持select关键字,用于处理异步IO 问题

  1. select {
  2. case <-chan1: // 如果chan1成功读到数据,则进行该case处理语句
  3.  
  4. case chan2 <- 1: // 如果成功向chan2写入数据,则进行该case处理语句
  5.  
  6. default: // 如果上面都没有成功,则进入default处理流程
  7. }

 

  1. // select.go
  2. package main
  3.  
  4. import (
  5. "fmt"
  6. "time"
  7. )
  8.  
  9. func main() {
  10. ch := make(chan int)
  11.  
  12. go func(ch chan int) {
  13. ch <- 1
  14. }(ch)
  15.  
  16. time.Sleep(time.Second)
  17.  
  18. select {
  19. case <-ch:
  20. fmt.Print("come to read ch!")
  21. default:
  22. fmt.Print("come to default!")
  23. }
  24. }

 

超时控制的经典实现:

  1. timeout := make(chan bool, 1)
  2. go func() {
  3. time.Sleep(1e9) // 等待1秒钟
  4. timeout <- true
  5. }()
  6.  
  7. // 然后我们把timeout这个channel利用起来
  8. select {
  9. case <-ch: // 从ch中读取到数据
  10. case <-timeout: // 一直没有从ch中读取到数据,但从timeout中读取到了数据
  11. }

 

  1. // select.go
  2. package main
  3.  
  4. import (
  5. "fmt"
  6. "time"
  7. )
  8.  
  9. func main() {
  10. ch := make(chan int)
  11. timeout := make(chan int, 1)
  12.  
  13. go func() {
  14. time.Sleep(time.Second)
  15. timeout <- 1
  16. }()
  17.  
  18. select {
  19. case <-ch:
  20. fmt.Print("come to read ch!")
  21. case <-timeout:
  22. fmt.Print("come to timeout!")
  23. }
  24.  
  25. fmt.Print("end of code!")
  26. }

 

还可以使用 time.After(…) 作为超时控制

  1. // select.go
  2. package main
  3.  
  4. import (
  5. "fmt"
  6. "time"
  7. )
  8.  
  9. func main() {
  10. ch := make(chan int)
  11.  
  12. select {
  13. case <-ch:
  14. fmt.Print("come to read ch!")
  15. case <-time.After(time.Second):
  16. fmt.Print("come to timeout!")
  17. }
  18.  
  19. fmt.Print("end of code!")
  20. }

 

协程与线程质的不同

协程的特点:

  • 该任务的业务代码主动要求切换,即主动让出执行权
  • 发生了IO,导致执行阻塞
  1. // goroutine_2.go
  2. package main
  3.  
  4. import (
  5. "fmt"
  6. "runtime"
  7. "strconv"
  8. "time"
  9. )
  10.  
  11. func main() {
  12. //协程1
  13. go func() {
  14. for i := 1; i < 100; i++ {
  15. if i == 10 {
  16. //主动出让cpu 使用的话 需要 导入 runtime包
  17. runtime.Gosched()
  18. }
  19. fmt.Println("routine 1:" + strconv.Itoa(i))
  20. }
  21. }()
  22.  
  23. //协程2
  24. go func() {
  25. for i := 100; i < 200; i++ {
  26. fmt.Println("routine 2:" + strconv.Itoa(i))
  27. }
  28. }()
  29.  
  30. time.Sleep(time.Second)
  31. }
  1. // goroutine
  2. package main
  3.  
  4. import (
  5. "fmt"
  6. "strconv"
  7. "time"
  8. )
  9.  
  10. var ch chan int
  11.  
  12. func main() {
  13. ch = make(chan int)
  14. //协程1
  15. go func() {
  16. for i := 1; i < 100; i++ {
  17. if i == 10 {
  18. //遇到了阻塞
  19. <-ch
  20. }
  21. fmt.Println("routine 1:" + strconv.Itoa(i))
  22. }
  23. }()
  24.  
  25. //协程2
  26. go func() {
  27. for i := 100; i < 200; i++ {
  28. fmt.Println("routine 2:" + strconv.Itoa(i))
  29. }
  30.  
  31. ch <- 1
  32. }()
  33.  
  34. time.Sleep(time.Second)
  35. }

java的多线程:

  1. public class thread {
  2. public static void main(String[] args){
  3. new Thread(new Test_thread()).start();
  4. new Thread(new Test_thread2()).start();
  5. }
  6. }
  7.  
  8. class Test_thread implements Runnable{
  9. public void run() {
  10. for (int i = 0; i < 100; i++) {
  11. System.out.println("thread 1:" + i);
  12. }
  13. }
  14. }
  15.  
  16. class Test_thread2 implements Runnable{
  17. public void run() {
  18. for (int i = 100; i < 200; i++) {
  19. System.out.println("thread 2:" + i);
  20. }
  21. }
  22. }

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Go