`

Java不常用关键字native、volatile、transient简介

    博客分类:
  • Java
阅读更多

Java目前共有50个关键字,如下所列:

 

abstract	   continue	 for           new     	switch
assert***   default	         goto*	package	synchronized
boolean	   do                  if	        private	this
break	   double	         int	        protected	throw
byte   	   else	         import	public	throws     case	
enum****   instanceof	 return	transient  catch	extends	
implements  short	         try           char	         final 	interface
static	    void              class	finally	 long	        strictfp**
volatile         const*	         float	native	 super	while
/*-----------------------华丽的分割线--------------------------------------------*/
*	 	not used
**	 	added in 1.2
***	 	added in 1.4
****	added in 5.0	

 

 其中goto和const为保留字,目前版本中不会使用,而常见的三个看似是关键字的true, false, null,都不是关键字,而是作为一个单独标识类型。 

 

虽然一直用java,不过很多关键字还是没见过的,然后用也用不明白的,自己稍稍连copy带写总结一下吧~~~

 

看到CSDN上有人概括的说道:

 

 写道
native
当你需要调用本地程序的时候

transient
你的类实现了java.io.Serializable而你又不想保存某个字段的时候

volatile
这个字段会被其他线程(直接或者间接)访问到,而你想保证每个线程都能得到最新的数据
(性能上肯定有损耗的,为了安全牺牲性能的事情多着去了)

 

很容易理解~~

下面再贴上写例子,可以供自己闲来没事敲一敲,加深理解:

 

1、Native

native是方法修饰符。Native方法是由另外一种语言(如c/c++,FORTRAN,汇编)实现的本地方法。一般用于JNI中。示例如下:

 

public class testdll  
{  
       static  
      {  
            System.loadLibrary("test");  
      }  

      public native static int get();  
      public native static void set(int i);  

      public static void main(String[] args)  
      {  
            testdll test = new testdll();  
            test.set(10);  
            System.out.println(test.get());  
      }  
}  

 

 test.dll可以用VC写,例如:

 

-----testdll.h-----
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class testdll */

#ifndef _Included_testdll
#define _Included_testdll
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class: testdll
 * Method: get
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_testdll_get  (JNIEnv *, jclass);

/*
 * Class: testdll
 * Method: set
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_testdll_set  (JNIEnv *, jclass, jint);

#ifdef __cplusplus
}
#endif
#endif


-------testdll.c-----------
#include "testdll.h"  

int i = 0;  

JNIEXPORT jint JNICALL Java_testdll_get (JNIEnv *, jclass)  
{  
       return i;  
}  

JNIEXPORT void JNICALL Java_testdll_set (JNIEnv *, jclass, jint j)  
{  
      i = j;  
}

 

2、transient

transient是一个变量修饰符,标记为transient的变量,在对一个对象进行序列化时,这些变量状态不会被序列化。

 

例如,假设某个类的成员变量是transient,那么当通过ObjectOutputStream把这个类的某个实例保存到磁盘上时,实际上transient变量的值是不会保存的。

 

当对象序列化的保存在存储器上时,不希望有些字段数据被保存,为了保证安全性,可以把这些字段声明为transient。

 

要更明白,可以看一些序列化的内容。

 

3、volatile

volatile修饰变量。

在每次被线程访问时,都强迫从共享内存中重读该成员变量的值。而且,当成员变量发生变化时,强迫线程将变化值回写到共享内存。这样在任何时刻,两个不同的线程总是看到某个成员变量的同一个值。  

看看Java Language Specification中的例子。  

条件:一个线程不停的调用方法one(),一个线程不停的调用方法two()。我测试过多次,这种情况好像一直没有出现。

class Test {   
  static int i = 0, j = 0;   

  static void one() { i++; j++; }   

  static void two() {   
       System.out.println("i=" + i + " j=" + j);   
  }   
}   

 

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

//代码
class Test {   
  static int i = 0, j = 0;   

  static synchronized void one() { i++; j++; }   

  static synchronized void two() {   
        System.out.println("i=" + i + " j=" + j);   
  }   
} 
 

这样可以防止两个方法同时被执行,还可以保证j和i被同时更新,这样一来i和j的值一直是一样的。  

另外一种途径就是把i和j声明为volatile。  

//代码
class Test {   
  static volatile int i = 0, j = 0;   

  static void one() { i++; j++; }  
 
  static void two() {   
            System.out.println("i=" + i + " j=" + j);   
  }   
}   
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics