多線程技術是JAVA ME中的關鍵技術
由於筆者對於游戲開發不是十分了解
多線程與聯網
多線程與拍照
Timer與TimerTask
多線程與聯網
手機中
下面
首先
以下是引用片段
/*
* NetworkConnection
*
* Created on
*
*/
package njunnection;
import java
import java
import java
import javax
import javax
/**
*
* @author Magic
*/
public class NetworkConnection {
private static final String URL = //localhost:
private HttpConnection httpConnection;
private String message;
public NetworkConnection(String message) {
ssage = message;
connect();
}
/**
* Connect to web server
*
*/
public void connect(){
try {
httpConnection = (HttpConnection) Connector
(HttpConnection
} catch (IOException ex) {
System
ex
}
}
/**
* Send message to server
* @throws java
*/
public void sendMessage() throws IOException{
DataOutputStream out = ();
out
out
}
/**
* Receive message from server
* @throws java
* @return
*/
public String receiveMessage() throws IOException {
DataInputStream in = ();
String message = in
in
return message;
}
/**
* Close connection
*/
public void close(){
if(httpConnection!=null){
try {
();
} catch (IOException ex) {
ex
}
}
}
}
構造函數的參數是將要被發送的消息
接著
以下是引用片段
/*
* MalConnectionMidlet
*
* Created on
*/
package njunnection;
import java
import javax
import javax
/**
*
* @author Magic
* @version
*/
public class MalConnectionMidlet extends MIDlet implements CommandListener {
private Display display;
private TextBox text;
private Command showCommand;
public MalConnectionMidlet(){
display = Display
text = new TextBox(
showCommand = new Command(
text
text
}
public void startApp() {
display
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command
if(command==showCommand){
/**
* 在當前的線程中直接進行聯網操作
*/
String message = null;
NetworkConnection connection = new NetworkConnection(
try {
connection
message = connection
connection
} catch (IOException ex) {
ex
}
text
}
}
}
當用戶按下
運行程序
圖
以下是引用片段
Warning: To avoid potential deadlock
networking
commandAction() handler
這就是因為沒有使用多線程造成的
[NextPage]
新建類NetworkThread
以下是引用片段
/*
* NetworkThread
*
* Created on
*
*/
package njunnection;
import java
import javax
/**
*
* @author Magic
*/
public class NetworkThread extends Thread {
private NetworkConnection connection;
private TextBox text;
public NetworkThread(TextBox text) {
super();
this
}
public void run() {
String message = null;
connection = new NetworkConnection(
try {
connection
message = connection
connection
} catch (IOException ex) {
ex
}
text
}
}
同時
以下是引用片段
/*
* ConnectionMidlet
*
* Created on
*/
package njunnection;
import java
import javax
import javax
/**
*
* @author Magic
* @version
*/
public class ConnectionMidlet extends MIDlet implements CommandListener {
private Display display;
private TextBox text;
private Command showCommand;
public ConnectionMidlet(){
display = Display
text = new TextBox(
showCommand = new Command(
text
text
}
public void startApp() {
display
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command
if(command==showCommand){
/**
* 創建新的線程完成聯網操作
*/
(new NetworkThread(text))
}
}
}
此時
圖
多線程與拍照
同樣
這是一個很簡單的攝像頭程序
圖
MalCameraMidlet和CameraMidlet分別是錯誤和正確的MIDlet
首先
以下是引用片段
/**
* MalCameraMidlet
*
*/
package nju
import javax
import javax
/**
* This MIDlet create the mal camera view
* will block after calling Capture command
* @author Magic
*
*/
public class MalCameraMidlet extends MIDlet {
protected Display display;
private CameraView camera;
public MalCameraMidlet() {
super();
display = Display
}
protected void startApp() {
camera = new MalCamera(this);
display
}
/**
* Show current camera
*/
public void showCamera(){
display
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg
}
}
CameraView是一個抽象類
以下是引用片段
/**
* CameraView
*/
package nju
import java
import javax
import javax
import javax
import javax
import javax
import jadia
import jadia
import jadia
import jantrol
import javax
/**
* This is an abstract class
* @author Magic
*
*/
public abstract class CameraView extends Form implements CommandListener {
protected MIDlet midlet;
protected Player player;
protected VideoControl vc;
protected Command exitCommand;
protected Command captureCommand;
protected CameraView(MIDlet midlet){
super(
this
exitCommand = new Command(
captureCommand = new Command(
addCommand(exitCommand);
addCommand(captureCommand);
setCommandListener(this);
/**
* Create camera player and control
*/
try {
player = Manager
player
vc = (VideoControl)player
append((Item)vc
player
} catch (IOException e) {
e
} catch (MediaException e) {
e
}
}
public abstract void commandAction(Command cmd
}
[NextPage]
MalCamera和Camera都繼承了CameraView類
以下是引用片段
/**
* MalCamera
*/
package nju
import javax
import javax
import jadia
import javax
/**
* This class display the mal camera
* for capture command
* new thread
* @author Magic
*
*/
public class MalCamera extends CameraView {
public MalCamera(MIDlet midlet){
super(midlet);
}
public void commandAction(Command cmd
if(cmd==exitCommand){
try {
player
} catch (MediaException e) {
e
}
player
((MalCameraMidlet)midlet)
midlet
}else if(cmd==captureCommand){
// Do not handle in a new thread
try {
byte[] data = vc
new SnapShot(midlet
} catch (MediaException e) {
e
}
}
}
}
其中SnapShot是顯示捕捉到的圖像的界面
現在運行MalCameraMidlet
圖
查看控制台窗口
以下是引用片段
Warning: To avoid potential deadlock
networking
commandAction() handler
同樣
以下是引用片段
/**
* Camera
*/
package nju
import javax
import javax
import jadia
import javax
/**
* This class displays camera
* for capture command
* @author Magic
*
*/
public class Camera extends CameraView {
public Camera(MIDlet midlet){
super(midlet);
}
public void commandAction(Command cmd
if(cmd==exitCommand){
try {
player
} catch (MediaException e) {
e
}
player
((CameraMidlet)midlet)
midlet
}else if(cmd==captureCommand){
// Handle in a new thread
new Thread(){
public void run(){
try {
byte[] data = vc
new SnapShot(midlet
} catch (MediaException e) {
e
}
}
}
}
}
}
同樣
以下是引用片段
/**
* CameraMidlet
*/
package nju
import javax
import javax
/**
* The correct MIDlet
* @author Magic
*
*/
public class CameraMidlet extends MIDlet {
protected Display display;
private CameraView camera;
public CameraMidlet() {
super();
display = Display
}
protected void startApp() {
camera = new Camera(this);
display
}
/**
* Show current camera
*/
public void showCamera(){
display
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg
}
}
運行CameraMidlet
圖
[NextPage]
Timer與TimerTask
聯網和拍照這兩種情況都需要程序員創建新的線程來完成任務
當然
TimerTask和Timer經常一起使用
這是一個計時的程序
以下是引用片段
/**
* ClockCanvas
*/
package nju
import java
import javax
import javax
import javax
import javax
import javax
import javax
/**
* This class display time to user
* to update time each second
* @author Magic
*
*/
public class ClockCanvas extends Canvas implements CommandListener {
private ClockMidlet midlet;
private Command exitCommand;
private Command stopCommand;
private Command resumeCommand;
private long second;
private int x;
private int y;
// Timer
private Timer timer;
public ClockCanvas(ClockMidlet midlet){
this
exitCommand = new Command(
stopCommand = new Command(
resumeCommand = new Command(
addCommand(exitCommand);
addCommand(stopCommand);
setCommandListener(this);
second =
x = getWidth()/
y = getHeight()/
// Create timer and start it
timer = new Timer();
timer
}
/**
* Add one second
*
*/
public void addSecond(){
second++;
}
protected void paint(Graphics g) {
g
g
g
g
// Draw second
g
}
public void commandAction(Command cmd
if(cmd==exitCommand){
timer
midlet
midlet
}else if (cmd==stopCommand){
timer
removeCommand(stopCommand);
addCommand(resumeCommand);
}else if (cmd==resumeCommand){
timer = null;
timer = new Timer();
timer
removeCommand(resumeCommand);
addCommand(stopCommand);
}
}
}
ClockCanvas繼承了Canvas
注意commandAction()方法
接著就來看看Clock是如何來工作的
以下是引用片段
/**
* Clock
*/
package nju
import java
/**
* Update the time
* @author Magic
*
*/
public class Clock extends TimerTask {
private ClockCanvas canvas;
public Clock(ClockCanvas canvas){
this
}
public void run() {
canvas
canvas
}
}
非常簡單
在加上下面的ClockMidlet就可以運行程序了
以下是引用片段
/**
* ClockMidlet
*/
package nju
import javax
import javax
import javax
/**
* Clock MIDlet
* @author Magic
*
*/
public class ClockMidlet extends MIDlet {
private Display display;
private Canvas clockCanvas;
public ClockMidlet() {
super();
display = Display
}
protected void startApp(){
clockCanvas = new ClockCanvas(this);
display
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg
}
}
運行程序
圖
總 結
以上介紹了多線程技術在聯網
由於筆者水平有限
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27462.html