在Java如何保证方法是线程安全的

 

废话开篇

都说Java程序好些,但是我觉得Java编程这东西,没个十年八年的真不敢说自己精通Java编程,由于工作原因,开始转战Java多线程,以前没怎么接触过,所以想留点脚印在这两条路上。

切入正题

开门见山,今天看到别人写的一段关于方法是否线程安全的文章,比较简单,但是由于自己也是刚开始入门,所以就迈下了第一步。由于注释还算比较详细,所以就不废话了,直接上code。

此方法不是线程安全的

 1 /**  
 2  * @Title: NotThreadSafeCounter.java
 3  * @Package never.uncategory
 4  * @Description: * The method is not thread-safe, because the 
 5  * counter++ operation is not atomic, which means it consists 
 6  * more than one atomic operations. In this case, one is 
 7  * accessing value and the other is increasing the value by one. 
 8  * @author "Never" xzllc2010#gmail.com  
 9  * @date Mar 14, 2014 7:44:24 PM
10  */
11 /*
12 
13  */
14 package never.uncategory;
15 
16 public class NotThreadSafeCounter extends Thread {
17 
18     private static int counter = 0;
19 
20     public void run() {
21         System.out.println("counter:" + getCount());
22     }
23 
24     public static int getCount() {
25 
26         try {
27             Thread.sleep(1500);
28         } catch (Exception e) {
29             e.printStackTrace();
30         }
31         return counter++;
32 
33     }
34 
35     public static void main(String[] args) {
36 
37         for (int i = 0; i < 5; i++) {
38             new NotThreadSafeCounter().start();
39         }
40     }
41 
42 }
View Code

此方法是线程安全的(synchronized)

 1 /**  
 2  * @Title: ThreadSafeCounter.java
 3  * @Package never.uncategory
 4  * @Description: Adding synchronized to this method will makes 
 5  * it thread-safe,When synchronized is added to a static method, 
 6  * the Class object is the object which is locked.If the method 
 7  * is not static, then adding synchronized keyword willsynchronize 
 8  * the instance of the class, not the Class object.
 9  * @author "Never" xzllc2010#gmail.com  
10  * @date Mar 14, 2014 8:09:45 PM
11  */
12 package never.uncategory;
13 
14 public class ThreadSafeCounter {
15 
16     private static int counter = 0;
17 
18     public void run() {
19         System.out.println("counter:" + getCount());
20     }
21 
22     public static synchronized int getCount() {
23         return counter++;
24     }
25 
26     public static void main(String[] args) {
27 
28         for (int i = 0; i < 5; i++) {
29             new NotThreadSafeCounter().start();
30         }
31     }
32 
33 }
View Code

此方法也是线程安全的(AtomicInteger)

 1 /**  
 2 * @Title: ThreadSafeCounter2.java
 3 * @Package never.uncategory
 4 * @Description: using AtomicInteger for safe counter
 5 * @author "Never" xzllc2010#gmail.com  
 6 * @date Mar 14, 2014 8:14:33 PM
 7 */ 
 8 package never.uncategory;
 9 
10 import java.util.concurrent.atomic.AtomicInteger;
11 
12 public class ThreadSafeCounter2 {
13 
14     private static AtomicInteger counter = new AtomicInteger(0);
15 
16     public void run() {
17         System.out.println("counter:" + getCount());
18     }
19 
20     public static int getCount() {
21         return counter.getAndIncrement();
22     }
23 
24     public static void main(String[] args) {
25 
26         for (int i = 0; i < 5; i++) {
27             new NotThreadSafeCounter().start();
28         }
29     }
30 }
View Code

时光的尾巴

刚入门,如有疑问,多指教和交流。

posted on 2014-03-14 20:31  RobertC  阅读(2821)  评论(0编辑  收藏  举报

导航