返回信息流从0开始才是王道。
0. 多线程。Java支持多线程。在C/C++之类的语言还依赖操作系统的接口的时候,Java就已经有了跨平台的语言级别的多线程支持。(C++11啊,你出生得太晚了,Java都等你16年了)。
package cn.byr.nuanyangyang.java20.thread;
public class ThreadTest {
public static void main(String[] args) throws Exception {
Thread t1 = new Thread(() -> {
for (int i = 0; i < 100; i++) {
System.out.println("Hello world " + i);
}
} );
Thread t2 = new Thread(() -> {
for (int i = 0; i < 100; i++) {
System.out.println("Goodbye world " + i);
}
} );
t1.start();
t2.start();
t1.join();
t2.join();
}
}
1. 动态装载。Java支持动态类装载。在C/C++之类的语言还依赖操作系统的接口的时候,Java已经有了跨平台的安全的好用的类装载器。在Java里,“ClassLoader + 类名”共同确定一个类,而不单单是类的名称。正因如此,程序员可以在程序运行时创建新的jar包,里面有和现有代码同名的类,然后创建不同的ClassLoader来读这个新的jar包。即使如此,也不会发生命名冲突(C程序员知道我是什么意思: http://bbs.byr.cn/#!article/Linux/147332 )
这是公共的接口
// IHelloWorld.jar
package cn.byr.nuanyangyang.java20.loader;
public interface IHelloWorld {
void greet(String name);
}
把下面这个具体的实现打到loadable-hw.jar包里去。
// LoadableHelloWorld.java
package cn.byr.nuanyangyang.java20.loader;
public class LoadableHelloWorld implements IHelloWorld {
@Override
public void greet(String name) {
System.out.println("Hello, " + name);
}
}
下面这是主程序:
package cn.byr.nuanyangyang.java20.loader;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
public class LoaderTest {
public static void main(String[] args) throws Exception {
File myJar = new File("loadable-hw.jar");
URLClassLoader cl = new URLClassLoader(new URL[] { myJar.toURI().toURL() });
Class<? extends IHelloWorld> lhwCls = cl.loadClass("cn.byr.nuanyangyang.java20.loader.LoadableHelloWorld")
.asSubclass(IHelloWorld.class);
IHelloWorld lhw = lhwCls.newInstance();
lhw.greet("world");
cl.close();
}
}
2. Unicode支持。Java支持Unicode,这对国际化非常有帮助。
package cn.byr.nuanyangyang.java20.unicode;
public class UnicodeTest {
public static void main(String[] args) {
System.out.println("\u4e16\u754c\u4f60\u597d");
}
}
3. 网络编程。Java支持网络编程,包括socket接口。当C/C++还在依赖操作系统提供的接口的时候,Java已经有了跨平台的网络编程支持。
package cn.byr.nuanyangyang.java20.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
class Client implements Runnable {
@Override
public void run() {
try (Socket s = new Socket("localhost", 8000)) {
System.out.println("[Client] Connected to server.");
try (InputStream is = s.getInputStream()) {
byte[] inBuffer = new byte[256];
int sz = 0;
int actualRead;
while ((actualRead = is.read(inBuffer, sz, 256 - sz)) > 0) {
sz += actualRead;
}
String str = new String(inBuffer, 0, sz, "UTF-8");
System.out.println("[Client] Received: " + str);
}
} catch (Exception e) {
System.out.println("An exception is thrown. There is really nothing I can do about it.");
e.printStackTrace();
}
}
}
class Server implements AutoCloseable {
private ServerSocket ss;
public Server() throws IOException {
ss = new ServerSocket(8000, 5);
}
public void serveOne() throws IOException {
try (Socket s = ss.accept()) {
System.out.println("[Server] A client connected.");
String word = "Hello world!";
byte[] wordBytes = word.getBytes("UTF-8");
try (OutputStream os = s.getOutputStream()) {
os.write(wordBytes);
}
System.out.println("[Server] Sent message to client.");
}
}
@Override
public void close() throws Exception {
if (ss != null) {
ss.close();
}
}
}
public class SocketTest {
public static void main(String[] args) throws Exception {
try (Server server = new Server()) {
Client client = new Client();
Thread clientThread = new Thread(client);
clientThread.start();
server.serveOne();
clientThread.join();
}
}
}
这是一条镜像帖。来源:北邮人论坛 / java / #41084同步于 2015/5/25
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
[Java20年]Quiz:下列特性是哪个Java版本引入的?(0)
nuanyangyang
2015/5/25镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复
4. 高级网络编程。Java自带URL和HTTP、FTP等协议的处理器。java.net里的URL可以用来打开各种协议的网络连接。但是,后来实践证明人们还是需要对具体协议(主要是HTTP)的更细粒度的控制的。所以,人们计划在Java1.9里提供更好的HTTP支持,包括HTTP2.0,不过这是后话了。Java支持URL和HTTP的时候,C/C++还依赖操作系统的接口,或者第三方的库呢。
package cn.byr.nuanyangyang.java20.URL;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class URLTest {
public static void main(String[] args) throws Exception {
URL url = new URL("http://m.byr.cn/");
URLConnection conn = url.openConnection();
try (InputStream is = conn.getInputStream()) {
byte[] inBuf = new byte[4096];
int actualRead;
while ((actualRead = is.read(inBuf)) > 0) {
System.out.write(inBuf, 0, actualRead);
}
}
}
}
5. Applet。Java有一个很奇怪的应用:嵌入在浏览器里运行。也许和那个时候,WWW刚刚起步有关,谁都想在Web上插一手。在和Netscape的配合下,Applet风行一时,但后来似乎人们不太喜欢Applet。现在Flash也有点衰落了,人们都开始玩HTML5和JavaScript。