📄 sender.java
字号:
package socket;import javax.microedition.midlet.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;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() { // 同步方法run while (true) { // 当没有客户端连接时,等待连接 if (message == null) { try { wait(); } catch (InterruptedException e) { } } // 唤醒后如果需发送的数据仍为空,则线程结束 if (message == null) break; try { os.write(message.getBytes()); os.write("\r\n".getBytes()); } catch (IOException ioe) { System.out.println("错误:"+ioe.getMessage()); } // 操作完成,将发送的数据设为空,进入等待状态 message = null; } } public synchronized void stop() { // 同步方法,用于停止线程 message = null; // 将需发送的数据设为空 notify(); // 唤醒线程 }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -