avatar
java 响应速度问题# Java - 爪哇娇娃
b*i
1
用我的JTextArea来显示文字,我循环显示1-20000,每显示一行都利用我写的函数来计
算屏幕缓冲区的字符,然后生成整个theText,然后设定到setText里面。
我让程序中在自定义的stop按钮按下时,我更改一个boolean emergency = true;
我这个循环其实每次都查询emergency变量,也设volatile了。但是,看到按下stop后
,程序继续运行了大约几秒钟后,才停止显示数字。
这个程序是java application. 没有使用线程。应该怎么做才能让程序在我按下stop时
立即停止呢?
avatar
h*0
2
没看懂……你没用线程,那是在哪里循环显示1-20000的?

【在 b***i 的大作中提到】
: 用我的JTextArea来显示文字,我循环显示1-20000,每显示一行都利用我写的函数来计
: 算屏幕缓冲区的字符,然后生成整个theText,然后设定到setText里面。
: 我让程序中在自定义的stop按钮按下时,我更改一个boolean emergency = true;
: 我这个循环其实每次都查询emergency变量,也设volatile了。但是,看到按下stop后
: ,程序继续运行了大约几秒钟后,才停止显示数字。
: 这个程序是java application. 没有使用线程。应该怎么做才能让程序在我按下stop时
: 立即停止呢?

avatar
g*g
3
GUI is always on another thread, otherwise when you are showing text,
your button won't respond to your click.
I don't know any way to force a thread to yield and another thread to
run immediately. If you use notify, it may reduce the latency.

【在 b***i 的大作中提到】
: 用我的JTextArea来显示文字,我循环显示1-20000,每显示一行都利用我写的函数来计
: 算屏幕缓冲区的字符,然后生成整个theText,然后设定到setText里面。
: 我让程序中在自定义的stop按钮按下时,我更改一个boolean emergency = true;
: 我这个循环其实每次都查询emergency变量,也设volatile了。但是,看到按下stop后
: ,程序继续运行了大约几秒钟后,才停止显示数字。
: 这个程序是java application. 没有使用线程。应该怎么做才能让程序在我按下stop时
: 立即停止呢?

avatar
h*0
4
should have Thread.yield(..), Thread.join(..)...

【在 g*****g 的大作中提到】
: GUI is always on another thread, otherwise when you are showing text,
: your button won't respond to your click.
: I don't know any way to force a thread to yield and another thread to
: run immediately. If you use notify, it may reduce the latency.

avatar
g*g
5
You can only ensure when the GUI thread gets there, it will
stop, but you can't guarantee when the GUI thead is scheduled.

【在 h*****0 的大作中提到】
: should have Thread.yield(..), Thread.join(..)...
avatar
c*t
6
It is very possible that you got cyclic triggering of events that caused
long delays and locking etc.
For example, when you call setText for instance, it will try to repaint,
and painting itself will trigger your action. Those all take a while.
There are many things you could try to optimize
1. obviously find a way to update the data w/o triggering redisplay
until ready for update.
2. Use immutable objects that eliminate the need for synchronization.

【在 b***i 的大作中提到】
: 用我的JTextArea来显示文字,我循环显示1-20000,每显示一行都利用我写的函数来计
: 算屏幕缓冲区的字符,然后生成整个theText,然后设定到setText里面。
: 我让程序中在自定义的stop按钮按下时,我更改一个boolean emergency = true;
: 我这个循环其实每次都查询emergency变量,也设volatile了。但是,看到按下stop后
: ,程序继续运行了大约几秒钟后,才停止显示数字。
: 这个程序是java application. 没有使用线程。应该怎么做才能让程序在我按下stop时
: 立即停止呢?

avatar
b*i
7
看了高手们的回帖,
是这样,我主程序里面一旦开始,就等待一个信号,Semaphore Start.
在我按下Start按钮后,Start.release();然后主程序得到信号,开始循环,显示,基本上每秒钟显示30行吧,不停卷屏。
按Stop,我设变量emergency = true;主程序的循环里面每次都检查这个变量。
现象是,按下stop后,基本上要好几秒以后才停止显示。有时甚至要十秒。
现在解决了,使用了thread就好了。就是说,主程序改成runable, void run(), start(){thread.....}等,这样循环就是thread里面的,java会分配更多时间片给UI了?

【在 b***i 的大作中提到】
: 用我的JTextArea来显示文字,我循环显示1-20000,每显示一行都利用我写的函数来计
: 算屏幕缓冲区的字符,然后生成整个theText,然后设定到setText里面。
: 我让程序中在自定义的stop按钮按下时,我更改一个boolean emergency = true;
: 我这个循环其实每次都查询emergency变量,也设volatile了。但是,看到按下stop后
: ,程序继续运行了大约几秒钟后,才停止显示数字。
: 这个程序是java application. 没有使用线程。应该怎么做才能让程序在我按下stop时
: 立即停止呢?

avatar
b*i
8
我觉得你说的也是我程序的一个很大问题,显示100行文字的时间我可以画几千个直线。
我的总体架构是这样:
一个ConsoleWindow keyboardIO extends JTextArea implements....
主程序可以向它设置setText();keyboardIO防在一个JPanel keyboardPane里面。
另外,主程序生成一个image类型的变量, 然后传给keyboardIO 记在hdc变量里。
keyboardPane.setDoubleBuffered(true);
然后,ConsoleWindow 的paint(){
. Rectangle theRect;
. theRect=this.getVisibleRect();
. this.scrollRectToVisible(theRect);
. if (theCanvas!=null)
. g.drawImage(theCanvas.images[nImage], theRect.x, theRect.y, this);
. super.paint(g);
. g.di

【在 c*****t 的大作中提到】
: It is very possible that you got cyclic triggering of events that caused
: long delays and locking etc.
: For example, when you call setText for instance, it will try to repaint,
: and painting itself will trigger your action. Those all take a while.
: There are many things you could try to optimize
: 1. obviously find a way to update the data w/o triggering redisplay
: until ready for update.
: 2. Use immutable objects that eliminate the need for synchronization.

avatar
c*t
9
If you really need performance, consider heavyweight components (i.e.
AWT). Of course, there are issues of mixing lightweight (i.e. Swing)
and heavyweight components, but usually can be overcomed.
I got a little confused. You said that you were drawing thousands of
lines... I don't know where that got fit in. If you were drawing the
image of thousands of lines. One thing you could do is to draw the
image in a separate thread, and then swap the background image. This
way, you can avoid clot

【在 b***i 的大作中提到】
: 我觉得你说的也是我程序的一个很大问题,显示100行文字的时间我可以画几千个直线。
: 我的总体架构是这样:
: 一个ConsoleWindow keyboardIO extends JTextArea implements....
: 主程序可以向它设置setText();keyboardIO防在一个JPanel keyboardPane里面。
: 另外,主程序生成一个image类型的变量, 然后传给keyboardIO 记在hdc变量里。
: keyboardPane.setDoubleBuffered(true);
: 然后,ConsoleWindow 的paint(){
: . Rectangle theRect;
: . theRect=this.getVisibleRect();
: . this.scrollRectToVisible(theRect);

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。