Home  >  Article  >  Java  >  Design 4 threads, two of which increase j by 1 each time, and the other two threads decrease j by 1 each time.

Design 4 threads, two of which increase j by 1 each time, and the other two threads decrease j by 1 each time.

PHP中文网
PHP中文网Original
2017-07-05 18:12:302356browse
<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> }

 

The above is the detailed content of Design 4 threads, two of which increase j by 1 each time, and the other two threads decrease j by 1 each time.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn