首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

多线程的例证,超级经典,可以学习学习

2012-09-14 
多线程的例子,超级经典,可以学习学习http://www.iteye.com/blogs/tag/java%E5%A4%9A%E7%BA%BF%E7%A8%8B服

多线程的例子,超级经典,可以学习学习
http://www.iteye.com/blogs/tag/java%E5%A4%9A%E7%BA%BF%E7%A8%8B

服务器端:

package thread;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;/** * 服务器端 * <p> * 服务器端打开侦听的端口,一有客户端连接就创建两个新的线程来负责这个连接 *  * 一个负责客户端发送的信息(ClientMsgCollectThread 类), *  * 另一个负责通过该Socket发送数据(ServerMsgSendThread ) *  * @author Faue */public class DTUServer extends ServerSocket {private static final int SERVER_PORT = 10000;private boolean isShakeHandsSuccess = false;/** * 构造方法,用于实现连接的监听 *  * @throws IOException */public DTUServer() throws IOException {super(SERVER_PORT);try {while (true) {Socket socket = super.accept();// 定时采集Thread thread = new Thread(new TimerThread(socket),"getAndShow" + socket.getPort());thread.start();// 发送指令new Thread(new SendInstructionThread(socket), "send"+ socket.getPort()).start();}} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) throws IOException {new DTUServer();}/** * 该类用于创建接收客户端发来的信息并显示的线程 * <p> * 相当于BDR定时 *  * @author Faue * @version 1.0.0 */class TimerThread implements Runnable {private Socket socket;private BufferedReader in;private StringBuffer inputStringBuffer = new StringBuffer("Hello");/** * 得到Socket的输入流 *  * @param s * @throws IOException */public TimerThread(Socket s) throws IOException {socket = s;System.out.println("定时采集");in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));}public void run() {try {while (!socket.isClosed()) {if (!isShakeHandsSuccess) {// 等待握手String s = in.readLine();System.out.println("s:" + s);if (s.equals("握手成功")) {isShakeHandsSuccess = true;}}if (isShakeHandsSuccess) {inputStringBuffer.delete(0, inputStringBuffer.length());System.out.println("阻塞线程,等待客户端发来消息");inputStringBuffer.append(in.readLine());System.out.println(getMsg(inputStringBuffer.toString()));}System.out.println("---------------");}} catch (Exception e) {// e.printStackTrace();System.out.println(socket.toString() + " is closed!");}}/** * 构造显示的字符串 *  * @param line * @return */private String getMsg(String line) {return socket.toString() + " 接收数据:" + line;}}/** * 该类用于创建发送数据的线程 *  * @author Faue * @version 1.0.0 */class SendInstructionThread implements Runnable {private Socket client;private PrintWriter out;private BufferedReader keyboardInput;private StringBuffer outputStringBuffer = new StringBuffer("Hello");/** * 得到键盘的输入流 *  * @param s * @throws IOException */public SendInstructionThread(Socket s) throws IOException {client = s;System.out.println("ServerMsgSendThread");out = new PrintWriter(client.getOutputStream(), true);keyboardInput = new BufferedReader(new InputStreamReader(System.in));}public void run() {try {while (!client.isClosed()) {outputStringBuffer.delete(0, outputStringBuffer.length());System.out.println("开始发送指令...");outputStringBuffer.append(keyboardInput.readLine());System.out.println("发送指令结束...");out.println(outputStringBuffer.toString());}} catch (IOException e) {// e.printStackTrace();System.out.println(client.toString() + " is closed!");}}}}


客户端:
package thread;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;/** * 客户端 * <p> * 实现基于IP地址的连接,连接后也创建两个线程来实现信息的发送和接收 *  * @author Faue */public class Client {private Socket mySocket;/** * 创建线程的构造方法 *  * @param IP * @throws IOException */public Client(String IP) throws IOException {try {mySocket = new Socket(IP, 10000);// 主线程还没有往下走shakeHands(mySocket);new Thread(new ServerMsgCollectThread(mySocket), "getAndShow"+ mySocket.getPort()).start();new Thread(new ClientMsgSendThread(mySocket), "send"+ mySocket.getPort()).start();} catch (IOException e) {// e.printStackTrace();System.out.println("Server.IP:" + IP+ " port:10000 can not be Connected");}}public static void main(String[] args) throws IOException {try {new Client("127.0.0.1");} catch (Exception e) {System.out.println("输入的IP地址错误");}}/** * 该类用于创建接收服务端发来的信息并显示的线程 *  * @author Faue * @version 1.0.0 */class ServerMsgCollectThread implements Runnable {private Socket client;private BufferedReader in;private StringBuffer inputStringBuffer = new StringBuffer("Hello");/** * 得到Socket的输入流 *  * @param s * @throws IOException */public ServerMsgCollectThread(Socket s) throws IOException {client = s;in = new BufferedReader(new InputStreamReader(client.getInputStream(), "GBK"));}public void run() {try {System.out.println("客户端开始接收循环.....");while (!client.isClosed()) {inputStringBuffer.delete(0, inputStringBuffer.length());System.out.println("等待读入数据.....");inputStringBuffer.append(in.readLine());System.out.println(getMsg(inputStringBuffer.toString()));}} catch (IOException e) {// e.printStackTrace();System.out.println(client.toString() + " is closed!");System.exit(0);}}/** * 构造输入字符串 *  * @param line * @return */private String getMsg(String line) {return client.toString() + " says:" + line;}}/** * 该类用于创建发送数据的线程 *  * @author Faue * @version 1.0.0 */class ClientMsgSendThread implements Runnable {private Socket client;private PrintWriter out;private BufferedReader keyboardInput;private StringBuffer outputStringBuffer = new StringBuffer("Hello");/** * 得到键盘的输入流 *  * @param s * @throws IOException */public ClientMsgSendThread(Socket s) throws IOException {client = s;out = new PrintWriter(client.getOutputStream(), true);keyboardInput = new BufferedReader(new InputStreamReader(System.in));}public void run() {try {while (!client.isClosed()) {outputStringBuffer.delete(0, outputStringBuffer.length());System.out.println("客户端等待写入数据.....");outputStringBuffer.append(keyboardInput.readLine());out.println(outputStringBuffer.toString());}out.println("--- See you, bye! ---");} catch (Exception e) {// e.printStackTrace();System.out.println(client.toString() + " is closed!");System.exit(0);}}}private void shakeHands(Socket s) {try {BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), "GBK"));StringBuffer outputStringBuffer = new StringBuffer("");PrintWriter out = new PrintWriter(s.getOutputStream(), true);while (true) {String hand = in.readLine();System.out.println("服务器发过来的握手信息" + hand);if (hand != null && "FE".equals(hand)) {outputStringBuffer.delete(0, outputStringBuffer.length());outputStringBuffer.append("握手成功");out.println(outputStringBuffer.toString());break;}}} catch (IOException e) {e.printStackTrace();}}}
学习了

热点排行