sun.misc.Unsafe的各种神技

原创 2016年06月05日 14:35:47


1. sun.misc.Unsafe包下载


        http://download.csdn.net/detail/dfdsggdgg/9535347

2. 实例化私有类


 正常情况下没法实例化一个私有构造函数的类,但是Unsafe可以做到。
import java.lang.reflect.Field;

import sun.misc.Unsafe;

public class UnsafePlayer {
    
	public static void main(String[] args) throws Exception {  
	//通过反射实例化Unsafe
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);  
        Unsafe unsafe = (Unsafe) f.get(null);  
  
        //实例化Player
        Player player = (Player) unsafe.allocateInstance(Player.class); 
        player.setName("li lei");
        System.out.println(player.getName());
        
    }  
}  
  
class Player{ 
	
    private String name;
    
    private Player(){}
    
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

3. cas原子级操作&&通过内存偏移地址修改变量值

  
import java.lang.reflect.Field;

import sun.misc.Unsafe;

public class UnsafePlayer {
    
	public static void main(String[] args) throws Exception {  
		//通过反射实例化Unsafe
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);  
        Unsafe unsafe = (Unsafe) f.get(null);  
  
        //实例化Player
        Player player = (Player) unsafe.allocateInstance(Player.class); 
        player.setAge(18);
        player.setName("li lei");
        for(Field field:Player.class.getDeclaredFields()){
        	System.out.println(field.getName()+":对应的内存偏移地址"+unsafe.objectFieldOffset(field));
        }
        System.out.println("-------------------");
        
        int ageOffset= 8;
        //修改内存偏移地址为8的值(age),返回true,说明通过内存偏移地址修改age的值成功
        System.out.println(unsafe.compareAndSwapInt(player, ageOffset, 18, 20));
        System.out.println("age修改后的值:"+player.getAge());
        System.out.println("-------------------");
        
        //修改内存偏移地址为8的值,但是修改后不保证立马能被其他的线程看到。
        unsafe.putOrderedInt(player, 8, 33);
        System.out.println("age修改后的值:"+player.getAge());
        System.out.println("-------------------");
        
        //修改内存偏移地址为12的值,volatile修饰,修改能立马对其他线程可见
        unsafe.putObjectVolatile(player, 12, "han mei");
        System.out.println("name修改后的值:"+unsafe.getObjectVolatile(player, 12));
    }  
}  
  
class Player{ 
	
	private int age;
	
    private String name;
    
    private Player(){}
    
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
}


3. 创建超级数组


    java中数组的最大长度为Integer.MAX_VALUE,正常情况下如果想创建一个大于Integer.MAX_VALUE的数组是做不到的,但是Unsafe可以,通过对内存进行直接分配实现。
import java.lang.reflect.Field;

import sun.misc.Unsafe;

public class SuperArray {
        
    public static void main(String[] arg) throws Exception{
    	
    	//通过反射实例化Unsafe
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);  
         Unsafe unsafe = (Unsafe) f.get(null);  
    	
         //只要内存够大,可以把这个调大,大于Integer.MAX_VALUE
         long size = (long)Integer.MAX_VALUE/2 ;  
         long addr = unsafe.allocateMemory(size);
         System.out.println("unsafe address :"+addr);
         
    	 for (int i = 0; i < size; i++) {  
    		 unsafe.putByte(addr+i, (byte)6); 
    		 if(unsafe.getByte(addr+i) !=6){
    			 System.out.println("failed at offset");
    		 } 
    	}  
    }
    
}
把size调大,size = (long)Integer.MAX_VALUE*2,错误信息如下。
unsafe address :15817328
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x61cc0350, pid=31240, tid=31324
#
# JRE version: Java(TM) SE Runtime Environment (8.0_45-b14) (build 1.8.0_45-b14)
# Java VM: Java HotSpot(TM) Client VM (25.45-b02 mixed mode windows-x86 )
# Problematic frame:
# V[thread 30484 also had an error]
  [jvm.dll+0x40350]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\welab\workspace\Person\hs_err_pid31240.log


4.线程挂起与恢复


 将一个线程进行挂起是通过park方法实现的,调用 park后,线程将一直阻塞直到超时或者中断等条件出现。unpark可以终止一个挂起的线程,使其恢复正常。整个并发框架中对线程的挂起操作被封装在 LockSupport类中,LockSupport类中有各种版本pack方法,但最终都调用了Unsafe.park()方法。
public class LockSupport {  

    /**
     * 恢复阻塞线程
     */
    public static void unpark(Thread thread) {  
        if (thread != null)  
            unsafe.unpark(thread);  
    }  
  
    /**
     * 一直阻塞当前线程,调用Unsafe.park()方法
     */
    public static void park(Object blocker) {  
        Thread t = Thread.currentThread();  
        setBlocker(t, blocker);  
        unsafe.park(false, 0L);  
        setBlocker(t, null);  
    }  
    /**
     * 阻塞当前线程nanos纳秒
     */
    public static void parkNanos(Object blocker, long nanos) {  
        if (nanos > 0) {  
            Thread t = Thread.currentThread();  
            setBlocker(t, blocker);  
            unsafe.park(false, nanos);  
            setBlocker(t, null);  
        }  
    }  
  
    public static void parkUntil(Object blocker, long deadline) {  
        Thread t = Thread.currentThread();  
        setBlocker(t, blocker);  
        unsafe.park(true, deadline);  
        setBlocker(t, null);  
    }  
  
    /**
     * 一直阻塞当前线程
     */
    public static void park() {  
        unsafe.park(false, 0L);  
    }  
   
   /**
     * 阻塞当前线程nanos纳秒
     */
    public static void parkNanos(long nanos) {  
        if (nanos > 0)  
            unsafe.park(false, nanos);  
    }  
  
    public static void parkUntil(long deadline) {  
        unsafe.park(true, deadline);  
    }  
}  
最后看下阻塞和恢复的例子
import java.util.concurrent.locks.LockSupport;

public class Lock {
   
	public static void main(String[] args) throws InterruptedException {
		
		ThreadPark threadPark = new ThreadPark();
		threadPark.start();
		ThreadUnPark threadUnPark = new ThreadUnPark(threadPark);
		threadUnPark.start();
		//等待threadUnPark执行成功
		threadUnPark.join();
		System.out.println("运行成功....");
	}
	
	
  static  class ThreadPark extends Thread{
	  
	   public void run(){
		    System.out.println(Thread.currentThread() +"我将被阻塞在这了60s....");
		    //阻塞60s,单位纳秒  1s = 1000000000
			LockSupport.parkNanos(1000000000l*60);
			
			System.out.println(Thread.currentThread() +"我被恢复正常了....");
	   }
   }
   
  static  class ThreadUnPark extends Thread{
	
	   public Thread thread = null;
	   
	   public ThreadUnPark(Thread thread){
		   this.thread = thread;
	   }
	   public void run(){
		   
		    System.out.println("提前恢复阻塞线程ThreadPark");
		    //恢复阻塞线程
			LockSupport.unpark(thread);
		
	   }
   }
}



参考:http://blog.csdn.net/fenglibing/article/details/17138079   
            http://blog.csdn.net/dfdsggdgg/article/details/51538601
            http://blog.csdn.net/aesop_wubo/article/details/7537278
 


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

源码剖析之sun.misc.Unsafe

转载于:http://zeige.iteye.com/blog/1182571 首先介绍一下什么是Compare And Swap(CAS)?简单的说就是比较并交换。 CAS 操作包含三个...
  • zgmzyr
  • zgmzyr
  • 2013-05-08 21:52
  • 7763

使用OpenJDK来查看SunJDK不提供的部分源码

转自:http://cduym.iteye.com/blog/1738269   都知道SunJDK(不喜欢叫他OracleJDK)只提供了java和javax下大部分的源码,其他源码想看却...

ClassLoader 之 sun.misc.Launcher 源码分析

jdk的 扩展类加载器ExtClassLoader和系统类加载器AppClassLoader就是定义在Launcher类里边的,所有这个类有必要研究一下,自学,记载到这里 请看Launcher的源...

classload之java程序入口sun.misc.Launcher源码分析

java程序的入口就是sun.misc.Launcher了,这个类里面又继承了ExtClassLoader和AppClassLoader和bootstrap的url路径 可以看到Launcher类初始...

sun.misc.Unsafe的各种神技

转自:http://blog.csdn.net/dfdsggdgg/article/details/51543545sun.misc.Unsafe包下载http://download.csdn.net...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)