java线程安全— synchronized和volatile
package threadsafe; public class TranditionalThreadSynchronized {
public static void main(String[] args)
{
final Outputter output = new Outputter(); new Thread()
{
public void run()
{
output.output("zhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsan");
}
}.start(); new Thread()
{
public void run()
{
output.output("lisi");
}
}.start();
}
} class Outputter
{
public void output(String name)
{
for(int i = 0; i < name.length(); i++)
{
System.out.print(name.charAt(i));
}
}
}
zhangsanzhangsanzlihangsanzhangsasinzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsanzhangsan
之所以用这个多的“zhangsan”,只用一个“lisi”,是为了防止如果只使用一个“zhangsan”,可能cpu的计算能力很强,会在执行完第一个线程之后,程序还没有
来得及进行到第二个线程,此时可能会造成一种错觉:先执行第一个线程,再执行第二个线程,其实在这里不是这样子的,如果cpu的运算能力不强,可能会出现类似于“zhlainsigsan”的输出效果。
一、使用synchronized关键字
我们希望output方法被一个线程完整的执行完之后再切换到下一个线程,Java中使用synchronized保证一段代码在多线程执行时是互斥的,有两种用法:
1.使用synchronized将需要互斥的代码包含起来,并上一把锁。
package threadsafe; public class TranditionalThreadSynchronized {
public static void main(String[] args)
{
final Outputter output = new Outputter(); new Thread()
{
public void run()
{
output.output("zhangsan");
}
}.start(); new Thread()
{
public void run()
{
output.output("lisi");
}
}.start();
}
} class Outputter
{
public void output(String name)
{
synchronized(this)
{
for(int i = 0; i < name.length(); i++)
{
System.out.print(name.charAt(i));
}
}
}
}
在这里,使用synchronized将需要互斥的代码块包含起来了,加了一把锁。
{
synchronized (this) {
for(int i = 0; i < name.length(); i++) {
System.out.print(name.charAt(i));
}
}
}
2.将synchronized加在需要互斥的方法上。
class Outputter
{
public synchronized void output(String name)
{
for(int i = 0; i < name.length(); i++)
{
System.out.print(name.charAt(i));
}
}
}
synchronized修饰的方法或者代码块可以看成是一个原子操作。
每个锁对象(JLS中叫monitor)都有两个队列,一个是就绪队列,一个是阻塞队列,就绪队列存储了将要获得锁的线程,阻塞队列存储了被阻塞的线程,当一个线程被唤醒(notify)后,才会进入到就绪队列,等待CPU的调度,反之,当一个线程被wait后,就会进入阻塞队列,等待下一次被唤醒,当第一个线程执行输出方法时,获得同步锁,执行输出方法,恰好此时第二个线程也要执行输出方法,但发现同步锁没有被释放,第二个线程就会进入就绪队列,等待锁被释放。
1. 获得同步锁;
2. 清空工作内存;
3. 从主内存拷贝对象副本到工作内存;
4. 执行代码(计算或者输出等);
5. 刷新主内存数据;
6. 释放同步锁。
synchronized既保证了多线程的并发有序性,又保证了多线程的内存可见性。
二、使用volatile关键字(在共享变量前加上)
一个变量可以被volatile修饰,在这种情况下内存模型(主内存和线程工作内存)确保所有线程可以看到一致的变量值。
class Test {
static int i = 0, j = 0;
static synchronized void one() {
i++;
j++;
}
static synchronized void two() {
System.out.println("i=" + i + " j=" + j);
}
}
这时one方法和two方法再也不会并发的执行了,i和j的值在主内存中会一直保持一致,并且two方法输出的也是一致的。
另外的同步机制就是在共享变量之前加上volatile:
class Test {
static volatile int i = 0, j = 0;
static void one() {
i++;
j++;
}
static void two() {
System.out.println("i=" + i + " j=" + j);
}
}
但是加上volatile可以将共享变量i和j的改变直接响应到主内存中,这样保证了主内存中i和j的值一致性,然而在执行two方法时,在two方法获取到i的值和获取到j的值中间的这段时间,one方法也许被执行了好多次,导致j的值会大于i的值。所以volatile可以保证内存可见性,不能保证并发有序性。