strictfp 与 volatile

转自:http://hi.baidu.com/liyunqi1984/blog/item/0128921373d98b015aaf53eb.html

1. strictfp
修饰类和方法,意思是FP-strict,精确浮点,符合IEEE-754规范的。当一个class或interface用strictfp声明,内部所有的float和double表达式都会成为strictfp的。Interface method不能被声明为strictfp的,class的可以。
Example:

代码
  1. strictfp interface FPTest {   
  2.      void methodA();   
  3. }   
  4. class FPClass implements FPTest {   
  5.     public void methodA() {   
  6.       }   
  7.     public void methodB() {   
  8.   }   
  9.   public strictfp void methodC() {   
  10.   }   
  11. }   
  12. class FPClassB {   
  13.     strictfp void methodA() {   
  14.       }   
  15. }  

 

 

2.volatile
volatile修饰变量。在每次被线程访问时,都强迫从共享内存中重读该成员变量的值。而且,当成员变量发生变化时,强迫线程将变化值回写到共享内存。这样在任何时刻,两个不同的线程总是看到某个成员变量的同一个值。
看看Java Language Specification中的例子。
条件:一个线程不停的调用方法one(),一个线程不停的调用方法two()。我测试过多次,这种情况好像一直没有出现。

 

代码
  1. class Test {   
  2.     static int i = 0, j = 0;   
  3.     static void one() { i++; j++; }   
  4.     static void two() {   
  5.           System.out.println("i=" + i + " j=" + j);   
  6.       }   
  7. }  


结果偶尔会出现j大于i的情况,因为方法没有同步,所以会出现i和j可能不是一次更新。一种防止这种情况发生的办法就是声明两个方法为synchronized 的。

代码
  1. class Test {   
  2.     static int i = 0, j = 0;   
  3.     static synchronized void one() { i++; j++; }   
  4.     static synchronized void two() {   
  5.           System.out.println("i=" + i + " j=" + j);   
  6.       }   
  7. }  


这样可以防止两个方法同时被执行,还可以保证j和i被同时更新,这样一来i和j的值一直是一样的。
另外一种途径就是把i和j声明为volatile。

代码
  1. class Test {   
  2.     static volatile int i = 0, j = 0;   
  3.     static void one() { i++; j++; }   
  4.     static void two() {   
  5.           System.out.println("i=" + i + " j=" + j);   
  6.       }   
  7. }  

 

 

public void initData() { ComicReadActivity.super.initData(); z(getIntent()); throw(this.const.while(), false, false, false); try { if (y0.public(x())) { ((TextView) ((ViewStub) findViewById(0x7f09018a)).inflate().findViewById(0x7f0905fd)).setText("《" + y() + "》"); return; } } catch (Exception e) { e.printStackTrace(); } if (this.break == null) { this.break = new BatteryReceiver(this); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.intent.action.BATTERY_CHANGED"); intentFilter.addAction("android.intent.action.TIME_TICK"); registerReceiver(this.break, intentFilter); } package packageVar = package.else(); if ((packageVar.volatile == null || !packageVar.final() || package.do(packageVar.volatile, "bannerbottom_mh2") == null) ? false : true) { try { this.mAdViewBanner.setReadSource(lg.if); this.mAdViewBanner.for(this, "bannerbottom_mh2"); } catch (Exception e2) { e2.printStackTrace(); } } if (strictfp.do.do.try) { FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(-2, -2); layoutParams.topMargin = fh.const(5.0f) + t0.do().do; layoutParams.rightMargin = fh.const(10.0f); layoutParams.gravity = 8388613; this.mTopRightFreeAdBtn.setLayoutParams(layoutParams); this.mTopRightFreeAdBtn.setVisibility(0); this.mTopRightFreeAdBtn.setText(strictfp.do.do.do()); this.mTopRightFreeAdBtn.setOnClickListener(new d40(this)); if (ou.do("SP_READ_FREE_AD_RED_TAG_COMIC_KEY", true)) { this.instanceof = true; M(true); } } l(x(), false); long currentTimeMillis = System.currentTimeMillis(); this.native = currentTimeMillis; og.do("SP_READ_CLOSE_REPEAT_COMIC", Long.valueOf(currentTimeMillis)); } } 解释一下这段代码
03-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值