sender.java

来自「J2ME socket通信示例代码」· Java 代码 · 共 42 行

JAVA
42
字号
import java.io.*;

public class Sender extends Thread{
	private OutputStream os;
	private String message;
	public Sender(OutputStream os){
		this.os=os;
		start();
	}
	
	public synchronized void send(String msg){
		message=msg;
		notify();
	}
	public synchronized void run(){
		while(true){
			if(message==null){
				try{
					wait();
				}catch(InterruptedException e){
					e.printStackTrace();
				}
			}
			if(message==null){
				break;
			}
			try {
				os.write(message.getBytes());
				os.write("\r\n".getBytes());
			} catch (IOException e) {
				e.printStackTrace();
			}
			message=null;
		}
	}
	
	public synchronized void stop(){
		message=null;
		notify();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?