首页 > Java > java教程 > 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1

设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1

PHP中文网
发布: 2017-07-05 18:12:30
原创
2390 人浏览过
<span style="color: #008080"> 1</span> <span style="color: #0000ff">package</span><span style="color: #000000"> zzg;
</span><span style="color: #008080"> 2</span> 
<span style="color: #008080"> 3</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> ThreadTest {
</span><span style="color: #008080"> 4</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span><span style="color: #000000"> j;
</span><span style="color: #008080"> 5</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span><span style="color: #000000"> main(String[] arge){
</span><span style="color: #008080"> 6</span>     ThreadTest t1 = <span style="color: #0000ff">new</span><span style="color: #000000"> ThreadTest();
</span><span style="color: #008080"> 7</span>     Inc inc = t1.<span style="color: #0000ff">new</span><span style="color: #000000"> Inc();
</span><span style="color: #008080"> 8</span>     Dec dec = t1.<span style="color: #0000ff">new</span><span style="color: #000000"> Dec();
</span><span style="color: #008080"> 9</span>     <span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> i=0;i<2;i++<span style="color: #000000">){
</span><span style="color: #008080">10</span>         Thread t = <span style="color: #0000ff">new</span><span style="color: #000000"> Thread(inc);
</span><span style="color: #008080">11</span> <span style="color: #000000">        t.start();
</span><span style="color: #008080">12</span>         t = <span style="color: #0000ff">new</span><span style="color: #000000"> Thread(dec);
</span><span style="color: #008080">13</span> <span style="color: #000000">        t.start();
</span><span style="color: #008080">14</span> <span style="color: #000000">    }
</span><span style="color: #008080">15</span> <span style="color: #000000">}
</span><span style="color: #008080">16</span> <span style="color: #0000ff">private</span>  <span style="color: #0000ff">synchronized</span> <span style="color: #0000ff">void</span><span style="color: #000000"> inc(){
</span><span style="color: #008080">17</span>     j++<span style="color: #000000">;
</span><span style="color: #008080">18</span>     System.out.println(Thread.currentThread().getName() + "-inc:" +<span style="color: #000000"> j);
</span><span style="color: #008080">19</span> <span style="color: #000000">}
</span><span style="color: #008080">20</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">synchronized</span> <span style="color: #0000ff">void</span><span style="color: #000000"> dec(){
</span><span style="color: #008080">21</span>     j--<span style="color: #000000">;
</span><span style="color: #008080">22</span>     System.out.println(Thread.currentThread().getName() + "-inc:" +<span style="color: #000000"> j);
</span><span style="color: #008080">23</span> <span style="color: #000000">}
</span><span style="color: #008080">24</span> <span style="color: #0000ff">class</span> Inc <span style="color: #0000ff">implements</span><span style="color: #000000"> Runnable{
</span><span style="color: #008080">25</span>     <span style="color: #0000ff">public</span>  <span style="color: #0000ff">void</span><span style="color: #000000"> run(){
</span><span style="color: #008080">26</span>         <span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> i=0;i<100;i++<span style="color: #000000">){
</span><span style="color: #008080">27</span> <span style="color: #000000">            inc();
</span><span style="color: #008080">28</span> <span style="color: #000000">        }
</span><span style="color: #008080">29</span> <span style="color: #000000">    }
</span><span style="color: #008080">30</span> <span style="color: #000000">}
</span><span style="color: #008080">31</span> <span style="color: #0000ff">class</span> Dec <span style="color: #0000ff">implements</span><span style="color: #000000"> Runnable{
</span><span style="color: #008080">32</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> run(){
</span><span style="color: #008080">33</span>         <span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> i=0;i<100;i++<span style="color: #000000">){
</span><span style="color: #008080">34</span> <span style="color: #000000">            dec();
</span><span style="color: #008080">35</span> <span style="color: #000000">        }
</span><span style="color: #008080">36</span> <span style="color: #000000">    }
</span><span style="color: #008080">37</span> <span style="color: #000000">}
</span><span style="color: #008080">38</span> }
登录后复制

 

以上是设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板