学习一门语言,请从关键字开始。
很惭愧,学java三年了。工作也一年多了。平时自诩学的还可以,但是最近才发现自己基础太烂,所以打算梳理、学习一下,
assert 断言关键字
描述:
断言,c/c++ 中也有相应概念
用法:
int intX = 80;
System.out.println("intX is:\t" + intX);
assert intX == 100 : "intX is 100 is false";
System.out.println("passed");
注意事项, java默认关闭断言开关,需要用java -ea 类名 开启
volatile 易变关键字
“程度较轻的 synchronized
”;与 synchronized
块相比,volatile 变量所需的编码较少,并且运行时开销也较少,但是它所能实现的功能也仅是 synchronized
的一部分
用法
synchronized
实例
class TestVolatile {
int intMoney = 0;
TestVolatile() {
new EarnMoneyThread().start();
new SpendMoneyThread().start();
new EarnMoneyThread().start();
new SpendMoneyThread().start();
new EarnMoneyThread().start();
new SpendMoneyThread().start();
new EarnMoneyThread().start();
new SpendMoneyThread().start();
}
public synchronized void addMoney() {
intMoney += 1;
System.out.println("earned money ===, now money:\t" + intMoney);
}
public synchronized void minusMoney() {
intMoney -= 1;
System.out.println("spend money, now money:\t" + intMoney);
}
class EarnMoneyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
addMoney();
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class SpendMoneyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
minusMoney();
try {
Thread.sleep(6);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
strictfp
描述
strictfp 关键字可应用于类、接口或方法。使用 strictfp 关键字声明一个方法时,该方法中所有的float和double表达式都严格遵守FP-strict的限制,符合IEEE-754规范。当对一个类或接口使用 strictfp 关键字时,该类中的所有代码,包括嵌套类型中的初始设定值和代码,都将严格地进行计算。严格约束意味着所有表达式的结果都必须是 IEEE 754 算法对操作数预期的结果,以单精度和双精度格式表示。
用法:
如果你想让你的浮点运算更加精确,而且不会因为不同的硬件平台所执行的结果不一致的话,可以用关键字strictfp.
instanceof
示例:
TestVolatile t = new TestVolatile();
t = null;
boolean isInstance = t instanceof Object;
System.out.println("" + isInstance);
transient
描述:
java语言的关键字,变量修饰符,如果用transient声明一个
实例变量,当对象存储时,它的值不需要维持。
Java的serialization提供了一种持久化对象实例的机制。当持久化对象时,可能有一个特殊的对象数据成员,我们不想用serialization机制来保存它。为了在一个特定对象的一个域上关闭serialization,可以在这个域前加上关键字transient。当一个对象被序列化的时候,transient型变量的值不包括在序列化的表示中,然而非transient型的变量是被包括进去的。
示例:
class TestTransient implements Serializable{
private String username;
private transient int age;
TestTransient(String username,int age){
this.username = username;
this.age = age;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("username:\t");
sb.append(username);
sb.append("age:\t");
sb.append(age);
return sb.toString();
}
}
TestTransient tt = new TestTransient("张三", 120);
System.out.println(tt.toString());
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(new FileOutputStream("d://1.txt"));
oos.writeObject(tt);
oos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d://1.txt"));
tt = (TestTransient) ois.readObject();
System.out.println(tt.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
线程池:
描述:对于java线程的控制,
示例:这里只是最基本的线程池应用
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(1);
MyThread mt1 = new MyThread("111111");
MyThread mt2 = new MyThread("22222222");
MyThread mt3 = new MyThread("33333333");
MyThread mt4 = new MyThread("4444444444");
es.execute(mt1);
es.execute(mt2);
es.execute(mt3);
es.execute(mt4);
es.shutdown();
}
}
class MyThread extends Thread{
String name;
MyThread(String name){
this.name = name;
}
@Override
public void run(){
System.out.println(name + "正在执行");
}
}