sender.java

来自「人民邮电出版社的《J2ME手机开发入门》全部源代码」· Java 代码 · 共 58 行

JAVA
58
字号
/*
 * Sender.java
 *
 * Created on 2005年5月5日, 下午2:39
 */

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;
    boolean stopFlag;

    public Sender(OutputStream os) {
        this.os = os;
        stopFlag = false;
        start();
    }

    public synchronized void send(String msg) {
        message = msg;
        notify();
    }

    public synchronized void run() {
        while (!stopFlag) {
            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) {
                ioe.printStackTrace();
            }
            message = null;
        }
    }

    public synchronized void stop() {
        message = null;
        notify();
    }
}

⌨️ 快捷键说明

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