首页 > 其他 > 详细

线程同步的几种手段

时间:2014-02-20 05:06:14      阅读:314      评论:0      收藏:0      [点我收藏+]

在软件产品特别是大容量通信系统的研发过程中,线程安全的重要性是不言而喻的。通常我们会使用synchronized 这样的同步关键字来做线程同步.却忽略了这样做也会面临许多的问题,比如synchronized关键字同步方法\代码块等同步监视器只能是对像,而不是把一段代码或是函数当作锁。如果我们的代码对同步方法的类再实例化一次的时候,或者代码不注意引起这个类多次实例化了的时候并不能做到线程同步.(当然我们可以通过类同步来纠正,但是这样做无疑扩大了同步的范围,在系统并发量要求大的情况下,显然不合适)

所以了这种同步方式在简单的场情下可能不会觉察出什么问题,但是并发量大时在性能下必然是受影响的。就个人理解而言,线程同步至少存在以下几种方式.

1.将变量声名原子的,如AtomicInteger。值得注意的是,这个地方经常用有人误用volatile static int这样的形式来声明,这样是起不到线程安全作用的。如下面这种情况:

public class VolatileTest {

       public volatile static int count = 0;

       public static AtomicInteger atomCount = new AtomicInteger(0);

 

       public static void increment() {

              try {

                     Thread.sleep(1);

              } catch (InterruptedException e) {

                     e.printStackTrace();

              }

              count++;

              atomCount.getAndIncrement();

       }

 

       public static void main(String[] args) throws InterruptedException {

              /**同时启动1000个线程,去进行count++计算,看看实际结果*/

              for (int i = 0; i < 1000; i++) {

                     new Thread(new Runnable() {

                            @Override

                            public void run() {

                                   VolatileTest.increment();

                            }

                     }).start();

                     // Thread.sleep(2);

              }

 

              /**主线程休眠,尽量使得业务线程全部执行完*/

              Thread.sleep(5000);

              /**这里每次运行的值都有可能不同,可能为1000*/

              System.out.println("运行结果:count=" + count);

              /**每次结果为1000*/

              System.out.println("运行结果:atomCount=" + atomCount.get());

       }

}

 

2.使用Lock控制并发线程:

public class LockTest {

       public static int count = 0;

       public static Lock lock =new ReentrantLock();

 

       public static void increment() {

              lock.lock();

              try {

                     count++;

              } catch (Exception e) {

                     e.printStackTrace();

              }finally{

                     lock.unlock();

              }

       }

 

       public static void main(String[] args) throws InterruptedException {

              /**同时启动1000个线程,去进行count++计算,看看实际结果*/

              for (int i = 0; i < 1000; i++) {

                     new Thread(new Runnable() {

                            @Override

                            public void run() {

                                   LockTest.increment();

                            }

                     }).start();

              }

 

              /**主线程休眠,尽量使得业务线程全部执行完*/

              Thread.sleep(5000);

              /**每次相同同,均为1000*/

              System.out.println("运行结果:count=" + count);

       }

}

 

3.使用synchronized控制并发线程。略

线程同步的几种手段

原文:http://www.cnblogs.com/hnucdj/p/3556328.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!