java 同對象不同方法線程同步的研究

來源:互聯網
上載者:User
<span style="font-size:18px;"><span style="white-space:pre">1、</span><span style="font-family: 'Microsoft YaHei'; color: rgb(0, 0, 102);">在多線程中使用同一對象時,有可能我們不希望不同的方法在同一時刻被調用;我在工作中就碰到這樣的情況,一個共用的變數被同一對象的不同方法修改;而這些方法又可能在多個線程中使用;於是我便寫了一個簡單的測試程式,來驗證是否可以使用synchronized來做到同對象不同方法的的安全執行緒;</span></span>
<span style="font-size:18px;"></span>
</pre><pre name="code" class="java"><span style="font-size:18px;">public class sync {private int a = 0;public void addFun1() {a++;System.out.print(a + " ");}public void addFun2() {a++;System.out.print(a + " ");}public void addFun3() {synchronized (this) {a++;System.out.print(a + " ");}}public void addFun4() {synchronized (this) {a++;System.out.print(a + " ");}}}</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;">public class MutliThread1 extends Thread{<span style="white-space:pre"></span>private int ticket=100;//每個線程都擁有100張票<span style="white-space:pre"></span>private sync a;    MutliThread1(sync syn){        a = syn;    }    public void run(){        while(ticket>0){            a.addFun1();            ticket--;        }    }}</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;">public class MutliThread2 extends Thread{<span style="white-space:pre"></span>private int ticket=100;//每個線程都擁有100張票<span style="white-space:pre"></span>private sync a;    MutliThread2(sync syn){        a = syn;    }    public void run(){        while(ticket>0){            a.addFun2();            ticket--;        }    }}</span>
<span style="font-size:18px;">public class testSyn {<span style="white-space:pre"></span>private static sync mSyn = new sync();<span style="white-space:pre"></span>/**<span style="white-space:pre"></span> * @param args<span style="white-space:pre"></span> */<span style="white-space:pre"></span>public static void main(String[] args) {<span style="white-space:pre"></span>// TODO Auto-generated method stub<span style="white-space:pre"></span>System.out.println("hello world");<span style="white-space:pre"></span><span style="white-space:pre"></span>MutliThread1 one = new MutliThread1(mSyn);<span style="white-space:pre"></span>MutliThread1 two = new MutliThread1(mSyn);<span style="white-space:pre"></span><span style="white-space:pre"></span><span style="white-space:pre"></span>one.start();<span style="white-space:pre"></span>two.start();<span style="white-space:pre"></span>}}</span>
<span style="font-size:18px;">2、輸出:hello world1 <span style="color:#ff0000;"><strong>2 4 3</strong></span> 5 6 7 8 10 9 11 13 14 12 15 17 18 19 16 20 22 23 24 21 25 27 28 29 26 30 32 33 34 35 31 36 38 39 40 37 41 43 44 45 42 46 <strong><span style="color:#ff0000;">48 49 47</span></strong> 50</span>
<span style="font-size:18px;">52 53 54 55 51 56 58 59 60 57 61 63 64 65 62 66 68 69 70 67 71 73 74 72 75 76 77 79 80 81 82 78 83 85 86 87 84 88 90 91 89 92 94 95 93 96 98 99 97 100</span>
<span style="font-size:18px;">102 103 104 101 105 106 107 108 109 110 111 113 112 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 134 135 136 137 138 139</span>
<span style="font-size:18px;">133 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176</span>
<span style="font-size:18px;"> 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 </span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;">3、分析:根據以上輸出可知,sync類的函數addFun1與addFun2是線程不安全的</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;">4、修改類</span><pre name="code" class="java"><span style="font-size:18px;">MutliThread1為:</span>
<span style="font-size:18px;">public class MutliThread1 extends Thread{<span style="white-space:pre"></span>private int ticket=100;//每個線程都擁有100張票<span style="white-space:pre"></span>private sync a;    MutliThread1(sync syn){        a = syn;    }    public void run(){        while(ticket>0){            <strong><span style="color:#993399;">a.addFun3();</span></strong>            ticket--;        }    }}</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;">5、修改類</span>
<pre name="code" class="java"><span style="font-size:18px;">MutliThread2為:</span>


<span style="font-size:18px;">public class MutliThread2 extends Thread{<span style="white-space:pre"></span>private int ticket=100;//每個線程都擁有100張票<span style="white-space:pre"></span>private sync a;    MutliThread2(sync syn){        a = syn;    }    public void run(){        while(ticket>0){            <strong><span style="color:#990000;">a.addFun4();</span></strong>            ticket--;        }    }}</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;">6、輸出:</span>
<span style="font-size:18px;">hello world1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50</span>
<span style="font-size:18px;"> 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100</span>
<span style="font-size:18px;"> 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 </span>


7、分析:

比較兩次的輸出可以發現,第一次輸出的數字不是有序的,第二次輸出是有序的,由此可以證明“synchronized (this)”可以實現不同函數的安全執行緒。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.