一个简单测试showNotify和hideNotify的程序,但不明白结果为何这样

archer_xy 2006-03-03 03:10:42
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Test extends MIDlet
{
Display display;

public Test()
{
super();
}

protected void startApp() throws MIDletStateChangeException
{
MyCanvas canvas = new MyCanvas();
display = Display.getDisplay(this);
display.setCurrent(canvas);
}

protected void pauseApp()
{
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
}
}

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class MyCanvas extends Canvas implements Runnable
{

public int x;
public boolean b = true;
public Thread thread;
public void showNotify()
{
thread = new Thread(this);
thread.start();
}

public void hideNotify()
{
System.out.println("****");
b=false;
}

protected void paint(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0x000000);
g.drawString("hello",x+10,10,0);
}

public void run()
{
Thread cur = Thread.currentThread();
while(b && thread == cur)
{
System.out.println(x);
x++;
repaint();
try
{
Thread.sleep(500);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}


点模拟器的pause后结果如下:
0
1
2
3
4
****
再resume,所有打印信息如下:
0
1
2
3
4
****
****
0
1
2
3
有两点疑问
1.showNotify方法调用后,程序会自动初始话全局变量吗?为什么x又从0开始计数了。
2.为什么调用showNotify方法后,程序还会自动调用一次hideNotify方法,将****打印一遍。
...全文
175 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
archer_xy 2006-03-09
  • 打赏
  • 举报
回复
我明白了,查setCurrent api 得到的启示,其实是两个不同对象都想显示时,但只能显示一个。比如:对象1 shownotify了,然后对象2 shownotify,此时对象1 自动hidenotify。
prok 2006-03-07
  • 打赏
  • 举报
回复
晕死.lz好好看看j2me程序的生命周期.你resume回来的时候是要先执行startApp的
MyCanvas canvas = new MyCanvas();
这个你放到构造函数里去再试一下.
cxc014 2006-03-06
  • 打赏
  • 举报
回复
不会吧,LZ你的程序是不是没有贴对.
本程序中:
首先执行shownotify(),线程开始,执行run()
while为真,打印X,x++;
按下pause
执行hidenotify(),b 为false,run中while为假(b =false),run结束,线程随之结束
按下resume
执行shownotify();线程开始,执行run()
while为假(b =false),run结束,线程随之结束
但MIDlet没有退出


superman421 2006-03-06
  • 打赏
  • 举报
回复
模拟器的调用方式你弄清楚了没有?
archer_xy 2006-03-06
  • 打赏
  • 举报
回复
大侠们HELP ME
archer_xy 2006-03-03
  • 打赏
  • 举报
回复
模拟器试了,为什么模拟器resume过后打印2次showNotify,还有1次hideNotify?
theLast_ 2006-03-03
  • 打赏
  • 举报
回复
thread = new Thread(this);
new了一个线程,当然里面的变量又是初始值了.

按暂停会调用shownotify()
按恢复会调用shownotify()和hidenotify()

我试过好像真机和模拟器不大一样,而且不同版本模拟器调用情况也不一样.
superman421 2006-03-03
  • 打赏
  • 举报
回复
仔细检查一下你的程序是怎么调用的吧!
这段代码是不会发生X从头计数的,除非每次运行有一个新的对象产生。
yanhan0615 2006-03-03
  • 打赏
  • 举报
回复
楼主你的程序有点不太明确啊,你这样改造一下:
public void showNotify()
{
System.out.println("showNotify");
thread = new Thread(this);
thread.start();
}

public void hideNotify()
{
System.out.println("hideNotify");
b=false;
}
就可以发现结果是这样:
showNotify
0
1
2
3
4
5
hideNotify
showNotify
showNotify
hideNotify
0
1
2
3

因为重新调用了showNotify(),所以重新构造了一个线程,当然是从头开始啦
archer_xy 2006-03-03
  • 打赏
  • 举报
回复
啊??哪款游戏或者应用不用线程??
wmzsl 2006-03-03
  • 打赏
  • 举报
回复
呵呵!兄弟别研究这些啦:) 实际开发中太少用线程了!

能用到线程开发的一般很少很少,除非很牛的公司或者你是很牛的人

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧