返回信息流Java这20年改了好多。虽然很保守,但一直有新特性加进来。请甄别下面这些特性是哪个版本加进Java语言的。其中有些特性实际上属于JVM或者Java API,这里都归在一起了。
0. synchronized关键字。每个Java对象里都有一个隐藏的reentrant锁。synchronized语句可以获得这个锁,进行安全的互斥操作,还可以进行条件提醒。
package cn.byr.nuanyangyang.java20.synch;
class Foo {
int num = 0;
final int limit = 10;
public synchronized void foo() {
num = num + 1;
if (num >= limit) {
notifyAll();
}
}
public void bar() {
int mynum;
synchronized (this) {
mynum = num;
}
System.out.println(mynum);
}
public void baz() throws InterruptedException {
int mynum;
synchronized (this) {
while ((mynum = num) < limit) {
wait();
}
}
System.out.println("mynum = " + mynum + ", which is >= limit.");
}
}
public class SynchTest {
public static void main(String[] args) throws Exception {
final Foo f = new Foo();
f.foo();
f.bar();
Thread waiterThread = new Thread(() -> {
try {
System.out.println("Waiting for mynum to exceed limit...");
f.baz(); // May see mynum > 10. That's normal because
// waiterThread may actually start later.
System.out.println("Finished waiting.");
} catch (Exception e) {
System.out.println("Oops. I am interrupted! I should stop.");
}
});
waiterThread.start();
for (int i = 1; i < 15; i++) {
f.foo();
f.bar();
}
waiterThread.join();
}
}
1. reflect(反射):Java可以在运行时获得一个对象的类型信息,包括类的名称、父类、成员变量、方法,并可以动态地创建实例,也可以动态地调用方法。这使得Java在某些方面可以像Python一样的动态语言一样进行运行时动态操作。
package cn.byr.nuanyangyang.java20.reflect;
import java.lang.reflect.Method;
class Person {
private String name;
private int age;
private double salary;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
public class ReflectTest {
public static void main(String[] args) throws Exception {
Person p = new Person("Alice");
p.setAge(30);
p.setSalary(5000.0);
Class<? extends Person> personCls = p.getClass();
System.out.println(personCls.getName());
for (Method m : personCls.getMethods()) {
String name = m.getName();
if (name.startsWith("get") && m.getParameterCount() == 0) {
Object value = m.invoke(p);
System.out.format("%s: %s\n", name, value);
}
}
}
}
2. Swing图形界面:Java支持图形界面编程,适合创建跨平台的、多线程的图形界面程序。
package cn.byr.nuanyangyang.java20.swing;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class SwingTest {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Hello world");
final JLabel l = new JLabel();
l.setText("Counting here...");
f.add(l);
f.pack();
f.setVisible(true);
Thread countingThread = new Thread(() -> {
try {
for (int i = 0;; i++) {
final int j = i;
SwingUtilities.invokeLater(() -> {
l.setText(String.format("i = %d", j));
});
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// should stop
}
});
countingThread.setDaemon(true);
countingThread.start();
}
}
3. Proxy类:Java可以在运行时创建Proxy类,实现给定的接口。这样,可以通过反射的方式动态实现一个接口。比如,截获调用的方法,计入日志,或者转换成网络调用。
package cn.byr.nuanyangyang.java20.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
interface Calculator {
double add(double a, double b);
double sub(double a, double b);
double sqrt(double a);
}
public class ProxyTest {
public static void main(String[] args) throws Exception {
Calculator fakeCalc = (Calculator) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(),
new Class<?>[] { Calculator.class }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.format("You called method: %s\n", method.getName());
for (Object arg : args) {
System.out.format(" Arg: %s, type: %s\n", arg, arg.getClass());
}
return 42.0;
}
});
fakeCalc.add(1.0, 2.0);
fakeCalc.sub(3.0, 4.0);
fakeCalc.sqrt(2.0);
}
}
这是一条镜像帖。来源:北邮人论坛 / java / #41077同步于 2015/5/25
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
[Java 20年]Quiz: 下列特性是哪个Java版本引入的?(1)
nuanyangyang
2015/5/25镜像同步18 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
=.=赞从0开始。。。
【 在 nuanyangyang 的大作中提到: 】
: Java这20年改了好多。虽然很保守,但一直有新特性加进来。请甄别下面这些特性是哪个版本加进Java语言的。其中有些特性实际上属于JVM或者Java API,这里都归在一起了。
: 0. synchronized关键字。每个Java对象里都有一个隐藏的reentrant锁。synchronized语句可以获得这个锁,进行安全的互斥操作,还可以进行条件提醒。
: [code=java]
: ...................
【 在 noEasy 的大作中提到: 】
: 只知道JDK1.8中的拉姆达表达式,虽然我不会,只是听说
这里面大量利用了Java很新的特性,不过我只是想讨论一些复古的特性。