Maison > Java > javaDidacticiel > le corps du texte

Concevez 4 threads, dont deux augmentent j de 1 à chaque fois, et les deux autres threads diminuent j de 1 à chaque fois.

PHP中文网
Libérer: 2017-07-05 18:12:30
original
2350 Les gens l'ont consulté
<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> }
Copier après la connexion

 

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!