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;
}
}
}
}
}
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;
}
}
}
}
}