Java多线程开发七——线程安全的数据结构

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


            }

        }


    }


}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值