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

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

PHP中文网
Lepaskan: 2017-07-05 18:12:30
asal
2374 orang telah melayarinya
<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> }
Salin selepas log masuk

 

Atas ialah kandungan terperinci 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan