Last active
August 29, 2015 14:04
-
-
Save neetsdkasu/4467ac7daa4c7452d518 to your computer and use it in GitHub Desktop.
Ezアプリ(J)メモ、アプリ内でのシステムスレッドに関して(スレッドセーフなアプリを目指して) Canvas ver.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
スレッドセーフなアプリを作るため | |
システムスレッドに関する実験をしてみた | |
実験に使用したコードも添付する | |
アプリの状態遷移やキー入力などでどのスレッドが呼ばれるのか | |
3つのシステムスレッドを確認できた | |
スレッドA | |
MIDletのコンストラクタ生成 | |
スレッドB | |
MIDletのstartApp,destroyApp,pauseAppのイベント | |
スレッドC | |
CommandListenerのcommandActionのイベント | |
CanvasのkeyPressed,keyRepeated,keyReleased,paintのイベント | |
実験の出力結果 | |
EZAPLI(J) Emulator | |
Copyright 2010-2011 KDDI Corporation. All rights reserved. | |
MIDlet constructer: 223940028 | |
myapp.ThreatTest@1a4cfaaa 100 | |
MIDlet startApp: 481523300 | |
make thread: -131535512 | |
Canvas paint: -368117631 | |
GameCanvas keyReleased[-6]: -368117631 | |
MIDlet commandAction midlet.command: -368117631 | |
GameCanvas keyReleased[-7]: -368117631 | |
MIDlet commandAction canvas.command: -368117631 | |
switch commandAction: from midlet to canvas | |
GameCanvas keyReleased[-7]: -368117631 | |
Canvas commandAction canvas.command: -368117631 | |
GameCanvas keyReleased[-6]: -368117631 | |
Canvas commandAction midlet.command: -368117631 | |
switch commandAction: from canvas to midlet | |
GameCanvas keyPressed[53]: -368117631 | |
loop: -131535512 | |
GameCanvas keyReleased[53]: -368117631 | |
GameCanvas keyPressed[55]: -368117631 | |
GameCanvas keyReleased[55]: -368117631 | |
GameCanvas keyPressed[54]: -368117631 | |
GameCanvas keyReleased[54]: -368117631 | |
GameCanvas keyPressed[54]: -368117631 | |
GameCanvas keyRepeated[54]: -368117631 | |
GameCanvas keyReleased[54]: -368117631 | |
myapp.ThreatTest@1a4cfaaa 103 | |
MIDlet pauseApp: 481523300 | |
myapp.ThreatTest@1a4cfaaa 0 | |
myapp.ThreatTest@1a4cfaaa 0 | |
myapp.ThreatTest@1a4cfaaa 100 | |
MIDlet startApp: 481523300 | |
Canvas paint: -368117631 | |
myapp.ThreatTest@1a4cfaaa 103 | |
MIDlet pauseApp: 481523300 | |
myapp.ThreatTest@1a4cfaaa 0 | |
myapp.ThreatTest@1a4cfaaa 0 | |
myapp.ThreatTest@1a4cfaaa 7 | |
MIDlet destroyApp: 481523300 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package myapp; | |
import java.util.Random; | |
import javax.microedition.lcdui.Command; | |
import javax.microedition.lcdui.CommandListener; | |
import javax.microedition.lcdui.Displayable; | |
import javax.microedition.lcdui.Graphics; | |
import javax.microedition.lcdui.game.GameCanvas; | |
public class TestCanvas extends GameCanvas implements Runnable, CommandListener { | |
static Command command = null; | |
Random random = null; | |
public TestCanvas() { | |
super(false); | |
command = new Command("CANV", Command.SCREEN, 2); | |
addCommand(command); | |
random = new Random(); | |
} | |
public void run() { | |
Graphics graphics = getGraphics(); | |
long wait0, wait1; | |
for (;;) { | |
wait0 = System.currentTimeMillis() + 200L; | |
int keyState = getKeyStates(); | |
if (keyState == FIRE_PRESSED) { | |
System.out | |
.println("loop: " + Thread.currentThread().hashCode()); | |
} else if (keyState == GAME_C_PRESSED) { | |
int x0 = random.nextInt(240); | |
int y0 = random.nextInt(240); | |
int x1 = random.nextInt(240); | |
int y1 = random.nextInt(240); | |
int r = 0xFF >> (random.nextInt(3) * 8); | |
int g = 0xFF >> (random.nextInt(3) * 8); | |
int b = 0xFF >> (random.nextInt(3) * 8); | |
graphics.setColor(r, g, b); | |
graphics.drawLine(x0, y0, x1, y1); | |
flushGraphics(); | |
} | |
do { | |
wait1 = System.currentTimeMillis(); | |
} while (wait1 < wait0); | |
} | |
} | |
protected void keyPressed(int keyCode) { | |
System.out.println("GameCanvas keyPressed[" + keyCode + "]: " | |
+ Thread.currentThread().hashCode()); | |
super.keyPressed(keyCode); | |
} | |
public void commandAction(Command c, Displayable d) { | |
if (c == command) { | |
System.out.println("Canvas commandAction canvas.command: " | |
+ Thread.currentThread().hashCode()); | |
} | |
if (c == ThreadTest.command) { | |
System.out.println("Canvas commandAction midlet.command: " | |
+ Thread.currentThread().hashCode()); | |
setCommandListener(ThreadTest.instance); | |
System.out.println("switch commandAction: from canvas to midlet"); | |
} | |
} | |
protected void keyReleased(int keyCode) { | |
System.out.println("GameCanvas keyReleased[" + keyCode + "]: " | |
+ Thread.currentThread().hashCode()); | |
super.keyReleased(keyCode); | |
} | |
protected void keyRepeated(int keyCode) { | |
System.out.println("GameCanvas keyRepeated[" + keyCode + "]: " | |
+ Thread.currentThread().hashCode()); | |
super.keyRepeated(keyCode); | |
} | |
public void paint(Graphics g) { | |
System.out.println("Canvas paint: " + Thread.currentThread().hashCode()); | |
super.paint(g); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package myapp; | |
import javax.microedition.lcdui.Command; | |
import javax.microedition.lcdui.CommandListener; | |
import javax.microedition.lcdui.Display; | |
import javax.microedition.lcdui.Displayable; | |
import javax.microedition.midlet.MIDlet; | |
import javax.microedition.midlet.MIDletStateChangeException; | |
public class ThreadTest extends MIDlet implements CommandListener { | |
static ThreadTest instance = null; | |
static Command command = null; | |
TestCanvas canvas = null; | |
Thread thread = null; | |
public ThreadTest() { | |
System.out.println("MIDlet constructer: " | |
+ Thread.currentThread().hashCode()); | |
command = new Command("MIDL", Command.SCREEN, 1); | |
canvas = new TestCanvas(); | |
canvas.addCommand(command); | |
canvas.setCommandListener(this); | |
Display.getDisplay(this).setCurrent(canvas); | |
} | |
protected void destroyApp(boolean arg0) throws MIDletStateChangeException { | |
System.out.println("MIDlet destroyApp: " | |
+ Thread.currentThread().hashCode()); | |
} | |
protected void pauseApp() { | |
System.out.println("MIDlet pauseApp: " | |
+ Thread.currentThread().hashCode()); | |
} | |
protected void startApp() throws MIDletStateChangeException { | |
System.out.println("MIDlet startApp: " | |
+ Thread.currentThread().hashCode()); | |
if (thread == null) { | |
instance = this; | |
thread = new Thread(canvas); | |
System.out.println("make thread: " + thread.hashCode()); | |
thread.start(); | |
} | |
} | |
public void commandAction(Command c, Displayable d) { | |
if (c == command) { | |
System.out.println("MIDlet commandAction midlet.command: " | |
+ Thread.currentThread().hashCode()); | |
} | |
if (c == TestCanvas.command) { | |
System.out.println("MIDlet commandAction canvas.command: " | |
+ Thread.currentThread().hashCode()); | |
canvas.setCommandListener(canvas); | |
System.out.println("switch commandAction: from midlet to canvas"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment