j2me开发精解源代码
源代码在线查看: sender.java
package com.j2medev.ch6.socket;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class Sender extends Thread {
private DataOutputStream dos;
private String message;
public Sender(DataOutputStream dos) {
this.dos = dos;
start();
}
public synchronized void send(String msg) {
message = msg;
//唤醒线程,发送数据
notify();
}
public synchronized void run() {
while (true) {
//如果message=null那么等待
if (message == null) {
try {
wait();
} catch (InterruptedException e) {
}
}
if (message == null) {
break;
}
try {
dos.writeUTF(message);
} catch (IOException ioe) {
ioe.printStackTrace();
}
message = null;
}
}
//停止
public synchronized void stop() {
message = null;
notify();
}
}