multithread_codes_java_eclipse.zip

  • 小磐石
    了解作者
  • Java
    开发工具
  • 198KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 1
    下载次数
  • 2017-05-25 17:01
    上传日期
这是好几个java多线程的学习程序,主要测试多线程的同步,join,以及实现多线程的两种方法(继承Thread类,实现runnable接口),代码完全正确,但是仅仅适用于多线程的初学者,用于学习,不用于具体工程
multithread_codes_java_eclipse.zip
  • multithread_codes_java_eclipse.docx
    209.9KB
内容介绍
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8"> <meta name="generator" content="pdf2htmlEX"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <link rel="stylesheet" href="https://static.pudn.com/base/css/base.min.css"> <link rel="stylesheet" href="https://static.pudn.com/base/css/fancy.min.css"> <link rel="stylesheet" href="https://static.pudn.com/prod/directory_preview_static/6271740dd973ef42a44f0cb0/raw.css"> <script src="https://static.pudn.com/base/js/compatibility.min.js"></script> <script src="https://static.pudn.com/base/js/pdf2htmlEX.min.js"></script> <script> try{ pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({}); }catch(e){} </script> <title></title> </head> <body> <div id="sidebar" style="display: none"> <div id="outline"> </div> </div> <div id="pf1" class="pf w0 h0" data-page-no="1"><div class="pc pc1 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://static.pudn.com/prod/directory_preview_static/6271740dd973ef42a44f0cb0/bg1.jpg"><div class="t m0 x1 h2 y1 ff1 fs0 fc0 sc0 ls0 ws0">1</div><div class="c x0 y2 w2 h3"><div class="t m0 x2 h4 y3 ff2 fs1 fc0 sc0 ls0 ws0">1.<span class="_ _0"> </span><span class="ff3">&#22810;&#32447;&#31243;&#30340;&#21516;&#27493;&#65306;</span></div><div class="t m0 x3 h4 y4 ff3 fs1 fc0 sc0 ls0 ws0">&#31243;&#24207;&#65306;</div><div class="t m0 x2 h5 y5 ff4 fs2 fc1 sc0 ls0 ws0">/*</div><div class="t m0 x2 h6 y6 ff3 fs2 fc1 sc0 ls0 ws0">&#27492;&#31243;&#24207;&#26088;&#22312;&#20171;&#32461;&#22810;&#32447;&#31243;&#20013;&#30340;&#21516;&#27493;&#38382;&#39064;&#65292;&#23601;&#26159;&#24403;&#20004;&#20010;&#32447;&#31243;&#21516;&#26102;&#35775;&#38382;&#19968;&#29255;&#20869;&#23384;&#21333;&#20803;&#26102;&#30340;&#20114;&#26021;&#12290;</div><div class="t m0 x2 h6 y7 ff3 fs2 fc1 sc0 ls0 ws0">&#26412;&#31243;&#24207;&#20849;&#22235;&#20010;&#31867;&#65292;&#20854;&#20013;&#20004;&#20010;&#26159;&#32447;&#31243;&#65292;&#19968;&#20010;&#26159;&#23384;&#20648;&#22534;&#26632;&#31867;&#20316;&#20026;&#20869;&#23384;&#21333;&#20803;&#65292;&#19968;&#20010;&#26159;&#20027;&#31867;&#20316;&#20026;&#31243;&#24207;&#27979;&#35797;&#20837;&#21475;</div><div class="t m0 x2 h6 y8 ff3 fs2 fc1 sc0 ls0 ws0">&#65292;&#20004;&#20010;&#32447;&#31243;&#65288;&#35835;&#20889;&#65289;&#35775;&#38382;&#30456;&#21516;&#36164;&#28304;&#12290;</div><div class="t m0 x2 h5 y9 ff4 fs2 fc1 sc0 ls0 ws0">*/</div><div class="t m0 x2 h6 ya ff4 fs2 fc1 sc0 ls0 ws0">/**<span class="ff3">&#20027;&#31867;</span>*/</div><div class="t m0 x2 h7 yb ff5 fs2 fc2 sc0 ls0 ws0">package<span class="ff4 fc0"> com.wuman;</span></div><div class="t m0 x2 h6 yc ff5 fs2 fc2 sc0 ls0 ws0">public<span class="ff4 fc0"> </span>class<span class="ff4 fc0"> StackTest {<span class="fc1">//<span class="ff3">&#20027;&#31867;</span></span></span></div><div class="t m0 x2 h7 yd ff4 fs2 fc0 sc0 ls0 ws0"> <span class="ff5 fc2">public</span> <span class="ff5 fc2">static</span> <span class="ff5 fc2">void</span> main(Str<span class="_ _1"></span>ing[] args) {</div><div class="t m0 x2 h6 ye ff4 fs2 fc0 sc0 ls0 ws0"> <span class="_ _2"> </span>DataStack stack=<span class="ff5 fc2">new</span> DataStac<span class="_ _1"></span>k();<span class="fc1">//<span class="ff3">&#21019;&#24314;&#22534;&#26632;</span></span></div><div class="t m0 x2 h6 yf ff4 fs2 fc0 sc0 ls0 ws0"> WriterPerson wp=<span class="ff5 fc2">new</span> WriterPers<span class="_ _1"></span>on(stack);<span class="fc1">//<span class="ff3">&#21019;&#24314;&#20889;&#32447;&#31243;</span>,<span class="ff3">&#35835;&#20889;&#32447;&#31243;&#37117;&#26159;&#40664;&#35748;&#30340;&#26222;&#36890;&#20248;</span></span></div><div class="t m0 x2 h6 y10 ff3 fs2 fc1 sc0 ls0 ws0">&#20808;&#32423;&#65292;&#32423;&#21035;&#20026;<span class="_ _3"> </span><span class="ff4">5</span>&#65292;&#21019;&#24314;&#20197;&#21518;&#22788;&#20110;&#23601;&#32490;&#29366;&#24577;</div><div class="t m0 x2 h6 y11 ff4 fs2 fc0 sc0 ls0 ws0"> ReaderPerson rp=<span class="ff5 fc2">new</span> ReaderPers<span class="_ _1"></span>on(stack);<span class="fc1">//<span class="ff3">&#21019;&#24314;&#35835;&#32447;&#31243;</span></span></div><div class="t m0 x2 h6 y12 ff4 fs2 fc0 sc0 ls0 ws0"> wp.start();<span class="fc1">//<span class="ff3">&#21551;&#21160;&#32447;&#31243;&#65292;&#36716;&#20026;&#23601;&#32490;&#29366;&#24577;</span></span></div><div class="t m0 x2 h5 y13 ff4 fs2 fc0 sc0 ls0 ws0"> rp.start();</div><div class="t m0 x2 h5 y14 ff4 fs2 fc0 sc0 ls0 ws0"> }</div><div class="t m0 x2 h5 y15 ff4 fs2 fc0 sc0 ls0 ws0">}</div><div class="t m0 x2 h6 y16 ff4 fs2 fc0 sc0 ls0 ws0">/**<span class="ff3">&#22534;&#26632;&#31867;</span>*/</div><div class="t m0 x2 h7 y17 ff5 fs2 fc2 sc0 ls0 ws0">package<span class="ff4 fc0"> com.wuman;</span></div><div class="t m0 x2 h6 y18 ff5 fs2 fc2 sc0 ls0 ws0">public<span class="ff4 fc0"> </span>class<span class="ff4 fc0"> DataStack {<span class="fc1">//<span class="ff3">&#22534;&#26632;&#31867;</span></span></span></div><div class="t m0 x2 h6 y19 ff4 fs2 fc0 sc0 ls0 ws0"> <span class="ff5 fc2">private</span> <span class="ff5 fc2">int</span> <span class="fc3">index</span>=0;<span class="fc1">//<span class="ff3">&#19979;&#26631;</span></span></div><div class="t m0 x2 h6 y1a ff4 fs2 fc0 sc0 ls0 ws0"> <span class="ff5 fc2">private</span> <span class="ff5 fc2">char</span> <span class="fc3">data</span>[]=<span class="ff5 fc2">new</span> <span class="ff5 fc2">char</span>[<span class="_ _1"></span>10];<span class="fc1">//<span class="ff3">&#25968;&#25454;&#23384;&#20648;&#65292;&#23450;&#20041;&#19968;&#20010;&#25968;&#32452;&#29992;&#26469;&#20316;&#20026;&#23384;&#20648;&#21306;&#65292;&#35835;&#20889;&#32447;&#31243;&#37117;</span></span></div><div class="t m0 x2 h6 y1b ff3 fs2 fc1 sc0 ls0 ws0">&#26469;&#35775;&#38382;&#20197;&#27169;&#25311;&#20114;&#26021;</div><div class="t m0 x2 h6 y1c ff4 fs2 fc0 sc0 ls0 ws0"> <span class="ff5 fc2">public</span> <span class="ff5 fc2">synchronized</span> <span class="ff5 fc2">void</span> push<span class="_ _1"></span>(<span class="ff5 fc2">char</span> c) {<span class="fc1">//<span class="ff3">&#25918;&#20837;&#25968;&#25454;&#65292;&#20026;&#20102;&#30830;&#20445;&#22312;&#20219;&#20309;&#26102;&#38388;&#28857;&#19968;&#20010;&#20849;&#20139;&#30340;&#36164;</span></span></div><div class="t m0 x2 h6 y1d ff3 fs2 fc1 sc0 ls0 ws0">&#28304;&#21482;&#34987;&#19968;&#20010;&#32447;&#31243;&#20351;&#29992;&#65292;&#20351;&#29992;&#20102;&#8220;&#20114;&#26021;&#8221;&#65292;&#36825;&#37324;&#29992;&#30340;&#26159;&#31532;&#19968;&#31181;&#8212;&#8212;&#20114;&#26021;&#26041;&#27861;</div><div class="t m0 x2 h6 y1e ff4 fs2 fc0 sc0 ls0 ws0"> <span class="_ _2"> </span><span class="ff5 fc2">while</span>(<span class="fc3">index</span>==<span class="fc3">data</span>.<span class="fc3">length</span>){<span class="fc1">//<span class="ff3">&#21516;&#27493;&#26465;&#20214;</span></span></div><div class="t m0 x2 h7 y1f ff4 fs2 fc0 sc0 ls0 ws0"> <span class="_ _2"> </span> <span class="ff5 fc2">try</span>{</div><div class="t m0 x2 h6 y20 ff4 fs2 fc0 sc0 ls0 ws0"> <span class="_ _2"> </span> <span class="_ _2"> </span><span class="ff5 fc2">this</span>.wait();<span class="fc1">//<span class="ff3">&#31561;&#24453;</span></span></div><div class="t m0 x2 h7 y21 ff4 fs2 fc0 sc0 ls0 ws0"> <span class="_ _2"> </span> <span class="_ _2"> </span>}<span class="ff5 fc2">catch</span>(InterruptedException e){}</div><div class="t m0 x2 h5 y22 ff4 fs2 fc0 sc0 ls0 ws0"> <span class="_ _2"> </span>}</div><div class="t m0 x2 h6 y23 ff4 fs2 fc0 sc0 ls0 ws0"> <span class="_ _2"> </span><span class="ff5 fc2">this</span>.notify();<span class="fc1">//<span class="ff3">&#28608;&#27963;&#32447;&#31243;</span></span></div><div class="t m0 x2 h6 y24 ff4 fs2 fc0 sc0 ls0 ws0"> <span class="_ _2"> </span><span class="fc3">data</span>[<span class="fc3">index</span>]=c;<span class="fc1">//<span class="ff3">&#25918;&#20837;&#25968;&#25454;</span></span></div><div class="t m0 x2 h6 y25 ff4 fs2 fc0 sc0 ls0 ws0"> <span class="_ _2"> </span><span class="fc3">index</span>++;<span class="fc1">//<span class="ff3">&#20462;&#25913;&#19979;&#26631;</span></span></div><div class="t m0 x2 h6 y26 ff4 fs2 fc0 sc0 ls0 ws0"> <span class="_ _2"> </span>System.<span class="ff6 fc3">out</span>.println(<span class="fc4">"Input data: "</span>+c);<span class="fc1">//<span class="ff3">&#36755;&#20986;&#32467;&#26524;</span></span></div><div class="t m0 x2 h5 y27 ff4 fs2 fc0 sc0 ls0 ws0"> }</div><div class="t m0 x2 h6 y28 ff4 fs2 fc0 sc0 ls0 ws0"> <span class="ff5 fc2">public</span> <span class="ff5 fc2">synchronized</span> <span class="ff5 fc2">char</span> pop(<span class="_ _1"></span>){<span class="fc1">//<span class="ff3">&#21462;&#20986;&#25968;&#25454;&#65292;&#21017;&#35201;&#26377;&#36820;&#22238;&#20540;&#12290;</span>synchronized<span class="_ _3"> </span><span class="ff3">&#20851;&#38190;&#23383;&#23545;&#20849;&#20139;</span></span></div></div></div><div class="pi" data-data='{"ctm":[1.611850,0.000000,0.000000,1.611850,0.000000,0.000000]}'></div></div> </body> </html>
评论
    相关推荐