Volatile变量
volatile变量表示保证它必须是与主内存保持一致,它实际是"变量的同步", 也就是说对于volatile变量的操作是原子型的,如用在long 或 double变量前。
Java语言中变量被保存在主内存中,可以用于多线程间的通信,除此之外,每个线程拥有自己的工作内存。Volatile用于保证这个变量在主内存与线程的工作内存之间的同步。
public class Sample{
int i;
volatile int j;
………
}
见上例,当一个线程更改了i的值后,另一个线程读取到的i的值有可能不是更改后的值,因为线程在工作内存中更改的值有可能还没有同步到主内存。而在变量j上面就不存在这个问题,因为volatile保证了j从线程的工作内存到主内存的同步。
JDK1.5以后提供的数据结构
一堆原子变量,各种线程安全的List,Queue,Map等,不一一描述,可以看JDK文档。
以生产者/消费者模型示范一下。

public class BlockQueueSample ...{
BlockingQueue<String> resource = new LinkedBlockingQueue<String>(10);
AtomicInteger i = new AtomicInteger();
boolean exit = false;

public BlockQueueSample() ...{
Thread consumer = new Thread(new Consumer());
Thread producer = new Thread(new Producer());
consumer.start();
producer.start();
}

public void exit() ...{
this.exit = true;
}

public static void main(String[] args) ...{
BlockQueueSample sample = new BlockQueueSample();
try ...{
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) ...{
return;
}
sample.exit();
}

public class Consumer implements Runnable ...{

public void run() ...{
while (!exit) ...{
try ...{
System.out.println("Consumer take: " + resource.take());
} catch (InterruptedException e) ...{
return;
}
}
}
}

public class Producer implements Runnable ...{

public void run() ...{
while (!exit) ...{
String r = "Resource" + i.getAndIncrement();
System.out.println("Producer put: " + r);
try ...{
resource.put(r);
} catch (InterruptedException e) ...{
return;
}
}
}
}
}
本文深入探讨了Java中Volatile变量的作用原理及其如何确保线程间变量的同步更新。同时,介绍了JDK1.5引入的一系列原子变量类,通过生产者/消费者模型示例展示了如何使用这些工具类来实现线程安全的数据操作。
582

被折叠的 条评论
为什么被折叠?



