转:基于Java回顾之多线程同步的使用详解
在这篇文章里,我们关注线程同步的话题。这是比多线程更复杂,稍不留意,我们就会“掉到坑里”,而且和单线程程序不同,多线程的错误是否每次都出现,也是不固定的,这给调试也带来了很大的挑战
首先阐述什么是同步,不同步有什么问题,然后讨论可以采取哪些措施控制同步,接下来我们会仿照回顾网络通信时那样,构建一个服务器端的“线程池”,JDK为我们提供了一个很大的concurrent工具包,最后我们会对里面的内容进行探索。
为什么要线程同步?
说到线程同步,大部分情况下, 我们是在针对“单对象多线程”的情况进行讨论,一般会将其分成两部分,一部分是关于“共享变量”,一部分关于“执行步骤”。
共享变量
当我们在线程对象(Runnable)中定义了全局变量,run方法会修改该变量时,如果有多个线程同时使用该线程对象,那么就会造成全局变量的值被同时修改,造成错误。我们来看下面的代码:
复制代码 代码如下: 共享变量造成同步问题 class MyRunner implements Runnable { public int sum = 0;
public void run()
{
System.out.println(Thread.currentThread().getName() + " Start.");
for (int i = 1; i <= 100; i++)
{
sum += i;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " --- The value of sum is " + sum);
System.out.println(Thread.currentThread().getName() + " End.");
}
}
private static void sharedVaribleTest() throws InterruptedException { MyRunner runner = new MyRunner(); Thread thread1 = new Thread(runner); Thread thread2 = new Thread(runner); thread1.setDaemon(true); thread2.setDaemon(true); thread1.start(); thread2.start(); thread1.join(); thread2.join(); }
这个示例中,线程用来计算1到100的和是多少,我们知道正确结果是5050(好像是高斯小时候玩过这个?),但是上述程序返回的结果是10100,原因是两个线程同时对sum进行操作。
执行步骤
我们在多个线程运行时,可能需要某些操作合在一起作为“原子操作”,即在这些操作可以看做是“单线程”的,例如我们可能希望输出结果的样子是这样的:
复制代码 代码如下: 线程1:步骤1 线程1:步骤2 线程1:步骤3 线程2:步骤1 线程2:步骤2 线程2:步骤3
如果同步控制不好,出来的样子可能是这样的: 复制代码 代码如下: 线程1:步骤1 线程2:步骤1 线程1:步骤2 线程2:步骤2 线程1:步骤3 线程2:步骤3
这里我们也给出一个示例代码: 复制代码 代码如下: 执行步骤混乱带来的同步问题 class MyNonSyncRunner implements Runnable { public void run() { System.out.println(Thread.currentThread().getName() + " Start."); for(int i = 1; i <= 5; i++) { System.out.println(Thread.currentThread().getName() + " Running step " + i); try { Thread.sleep(50); } catch(InterruptedException ex) { ex.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + " End."); } }
private static void syncTest() throws InterruptedException { MyNonSyncRunner runner = new MyNonSyncRunner(); Thread thread1 = new Thread(runner); Thread thread2 = new Thread(runner); thread1.setDaemon(true); thread2.setDaemon(true); thread1.start(); thread2.start(); thread1.join(); thread2.join(); }
如何控制线程同步
既然线程同步有上述问题,那么我们应该如何去解决呢?针对不同原因造成的同步问题,我们可以采取不同的策略。
控制共享变量
我们可以采取3种方式来控制共享变量。
将“单对象多线程”修改成“多对象多线程”
上文提及,同步问题一般发生在“单对象多线程”的场景中,那么最简单的处理方式就是将运行模型修改成“多对象多线程”的样子,针对上面示例中的同步问题,修改后的代码如下:
复制代码 代码如下: 解决共享变量问题方案一 private static void sharedVaribleTest2() throws InterruptedException { Thread thread1 = new Thread(new MyRunner()); Thread thread2 = new Thread(new MyRunner()); thread1.setDaemon(true); thread2.setDaemon(true); thread1.start(); thread2.start(); thread1.join(); thread2.join(); }
我们可以看到,上述代码中两个线程使用了两个不同的Runnable实例,它们在运行过程中,就不会去访问同一个全局变量。 将“全局变量”降级为“局部变量”
既然是共享变量造成的问题,那么我们可以将共享变量改为“不共享”,即将其修改为局部变量。这样也可以解决问题,同样针对上面的示例,这种解决方式的代码如下:
复制代码 代码如下: 解决共享变量问题方案二 class MyRunner2 implements Runnable { public void run() { System.out.println(Thread.currentThread().getName() + " Start."); int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " --- The value of sum is " + sum); System.out.println(Thread.currentThread().getName() + " End."); } }
private static void sharedVaribleTest3() throws InterruptedException { MyRunner2 runner = new MyRunner2(); Thread thread1 = new Thread(runner); Thread thread2 = new Thread(runner); thread1.setDaemon(true); thread2.setDaemon(true); thread1.start(); thread2.start(); thread1.join(); thread2.join(); }
我们可以看出,sum变量已经由全局变量变为run方法内部的局部变量了。 使用ThreadLocal机制
ThreadLocal是JDK引入的一种机制,它用于解决线程间共享变量,使用ThreadLocal声明的变量,即使在线程中属于全局变量,针对每个线程来讲,这个变量也是独立的。
我们可以用这种方式来改造上面的代码,如下所示:
复制代码 代码如下: 解决共享变量问题方案三 class MyRunner3 implements Runnable { public ThreadLocal tl = new ThreadLocal();
public void run()
{
System.out.println(Thread.currentThread().getName() + " Start.");
for (int i = 0; i <= 100; i++)
{
if (tl.get() == null)
{
tl.set(new Integer(0));
}
int sum = ((Integer)tl.get()).intValue();
sum+= i;
tl.set(new Integer(sum));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " --- The value of sum is " + ((Integer)tl.get()).intValue());
System.out.println(Thread.currentThread().getName() + " End.");
}
}
private static void sharedVaribleTest4() throws InterruptedException { MyRunner3 runner = new MyRunner3(); Thread thread1 = new Thread(runner); Thread thread2 = new Thread(runner); thread1.setDaemon(true); thread2.setDaemon(true); thread1.start(); thread2.start(); thread1.join(); thread2.join(); }
综上三种方案,第一种方案会降低多线程执行的效率,因此,我们推荐使用第二种或者第三种方案。
控制执行步骤
说到执行步骤,我们可以使用synchronized关键字来解决它。
复制代码 代码如下: 执行步骤问题解决方案 class MySyncRunner implements Runnable { public void run() { synchronized(this) { System.out.println(Thread.currentThread().getName() + " Start."); for(int i = 1; i <= 5; i++) { System.out.println(Thread.currentThread().getName() + " Running step " + i); try { Thread.sleep(50); } catch(InterruptedException ex) { ex.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + " End."); } } }
private static void syncTest2() throws InterruptedException { MySyncRunner runner = new MySyncRunner(); Thread thread1 = new Thread(runner); Thread thread2 = new Thread(runner); thread1.setDaemon(true); thread2.setDaemon(true); thread1.start(); thread2.start(); thread1.join(); thread2.join(); }
在线程同步的话题上,synchronized是一个非常重要的关键字。它的原理和数据库中事务锁的原理类似。我们在使用过程中,应该尽量缩减synchronized覆盖的范围,原因有二:1)被它覆盖的范围是串行的,效率低;2)容易产生死锁。我们来看下面的示例: 复制代码 代码如下: synchronized示例 private static void syncTest3() throws InterruptedException { final List list = new ArrayList();
Thread thread1 = new Thread()
{
public void run()
{
System.out.println(Thread.currentThread().getName() + " Start.");
Random r = new Random(100);
synchronized(list)
{
for (int i = 0; i < 5; i++)
{
list.add(new Integer(r.nextInt()));
}
System.out.println("The size of list is " + list.size());
}
try
{
Thread.sleep(500);
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " End.");
}
};
Thread thread2 = new Thread()
{
public void run()
{
System.out.println(Thread.currentThread().getName() + " Start.");
Random r = new Random(100);
synchronized(list)
{
for (int i = 0; i < 5; i++)
{
list.add(new Integer(r.nextInt()));
}
System.out.println("The size of list is " + list.size());
}
try
{
Thread.sleep(500);
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " End.");
}
};
thread1.start();
thread2.start();
thread1.join();
thread2.join();
}
我们应该把需要同步的内容集中在一起,尽量不包含其他不相关的、消耗大量资源的操作,示例中线程休眠的操作显然不应该包括在里面。