• synchronized和volatile关键字


    synchronized

    同步块大家都比较熟悉,通过 synchronized 关键字来实现,所有加上synchronized 和 块语句,在多线程访问的时候,同一时刻只能有一个线程能够用 synchronized 修饰方法或者代码块。

    volatile

    用volatile修饰的变量,线程在每次使用变量的时候,都会读取变量修改后的值。volatile很容易被误用,用来进行原子性操作。

    以下两段程序演示了两者区别

    package net.dp.volatileTest;
    
    public class JoinThread extends  Thread  {
    	public static volatile int n = 0;
    
    	public void run() {
    		for (int i = 0; i < 10; i++)
    			try {
    				n = n + 1;
    				sleep(3); // 为了使运行结果更随机,延迟3毫秒
    
    			} catch (Exception e) {
    			}
    	}
    
    	public static void main(String[] args) throws Exception {
    
    		Thread threads[] = new Thread[100];
    		for (int i = 0; i < threads.length; i++)
    			// 建立100个线程
    			threads[i] = new JoinThread();
    		for (int i = 0; i < threads.length; i++)
    			// 运行刚才建立的100个线程
    			threads[i].start();
    		for (int i = 0; i < threads.length; i++)
    			// 100个线程都执行完后继续
    			threads[i].join();
    		System.out.println(" n= " + JoinThread.n);
    		//执行结果 n < 1000
    		
    	}
    }
    

    程序的运行结果是n小于1000.

    原因:Java在创建线程的时候会为每一个线程分配线程当前的内存空间用来存放当前线程中的变量,存储在栈中;这些变量最终的值是存储在堆中。也就是说栈中会有堆中变量值的副本。volatile关键字是保证不同线程对该变量的访问是从堆中获取,拿到的是同一个变量的真实值不是栈中的副本。但是这并不能保证该操作是原子性的。在程序中n = n + 1操作就不是原子性的所以才会出错。

    使用synchronized关键字解决该问题。

     

    package net.dp.volatileTest;
    
    public class JoinThread2 extends Thread {
    
    	public static int n = 0;
    
    	public static synchronized void inc() {
    		n++;
    	}
    
    	public void run() {
    		for (int i = 0; i < 10; i++)
    			try {
    				inc(); // n = n + 1 改成了 inc();
    				sleep(3); // 为了使运行结果更随机,延迟3毫秒
    
    			} catch (Exception e) {
    			}
    	}
    
    	public static void main(String[] args) throws Exception {
    
    		Thread threads[] = new Thread[100];
    		for (int i = 0; i < threads.length; i++)
    			// 建立100个线程
    			threads[i] = new JoinThread2();
    		for (int i = 0; i < threads.length; i++)
    			// 运行刚才建立的100个线程
    			threads[i].start();
    		for (int i = 0; i < threads.length; i++)
    			// 100个线程都执行完后继续
    			threads[i].join();
    		System.out.println(" n= " + JoinThread2.n);
    	}
    
    }
    

      

    程序执行结果是1000 

    使用synchronized关键字对自增操作修饰,保证了自增的原子操作。

      

  • 相关阅读:
    《架构师》反思:系统可靠性
    表现力(转)
    4月反思
    30天敏捷结果(10) 强化你的一周
    认真对待学习(2月反思)
    Sort By Double
    12月反思 组内设计评审会议
    WPF框架的内存泄漏BUG
    OpenExpressApp 框架结构(2)
    绑定子类的泛型基类,反模式?
  • 原文地址:https://www.cnblogs.com/lcqBlogs/p/4280715.html
Copyright © 2020-2023  润新知