java 同对象不同方法线程同步的研究

来源:互联网 发布:linux下more命令 编辑:程序博客网 时间:2024/05/07 03:41
<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)”可以实现不同函数的线程安全。

0 0
原创粉丝点击