`
softstone
  • 浏览: 461650 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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

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


            }

        }


    }


}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics