Heim >Java >javaLernprogramm >Entwerfen Sie 4 Threads, von denen zwei j jedes Mal um 1 erhöhen und die anderen beiden Threads j jedes Mal um 1 verringern.

Entwerfen Sie 4 Threads, von denen zwei j jedes Mal um 1 erhöhen und die anderen beiden Threads j jedes Mal um 1 verringern.

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

 

Das obige ist der detaillierte Inhalt vonEntwerfen Sie 4 Threads, von denen zwei j jedes Mal um 1 erhöhen und die anderen beiden Threads j jedes Mal um 1 verringern.. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn