由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - java 响应速度问题
相关主题
JTextArea做console窗口的目前结果和问题Intellij Idea gui not pure Swing?
keyPressed(KeyEvent e) 需要考虑重入吗getImage有个问题
Re: AWT 和 SWING 在程序应用中有什么区别?jvm是怎么implement monitor的?
JTextArea行间距如何设定?JTextPane怎么快速更新
怎样才能Firefox add-on和java程序之间传递数据Re: JTextArea的内容不滚动
GUI问题如何输出TXT文件?
question about repaint, update and paintRe: help with JTextArea
有没有类似MS Visual Studio的Java IDE?solvedRe: how to change the input mode?
相关话题的讨论汇总
话题: thread话题: gui话题: 显示话题: 主程序话题: stop
进入Java版参与讨论
1 (共1页)
b***i
发帖数: 3043
1
用我的JTextArea来显示文字,我循环显示1-20000,每显示一行都利用我写的函数来计
算屏幕缓冲区的字符,然后生成整个theText,然后设定到setText里面。
我让程序中在自定义的stop按钮按下时,我更改一个boolean emergency = true;
我这个循环其实每次都查询emergency变量,也设volatile了。但是,看到按下stop后
,程序继续运行了大约几秒钟后,才停止显示数字。
这个程序是java application. 没有使用线程。应该怎么做才能让程序在我按下stop时
立即停止呢?
h*****0
发帖数: 4889
2
没看懂……你没用线程,那是在哪里循环显示1-20000的?

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

g*****g
发帖数: 34805
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时
: 立即停止呢?

h*****0
发帖数: 4889
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.

g*****g
发帖数: 34805
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(..)...
c*****t
发帖数: 1879
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时
: 立即停止呢?

b***i
发帖数: 3043
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时
: 立即停止呢?

b***i
发帖数: 3043
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.

c*****t
发帖数: 1879
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);

s********o
发帖数: 2
10
put time consuming job in SwingWorker, free the event dispatch thread so
that when you push the button, it will response quickly
refer java tutorial concurrency:
http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html
1 (共1页)
进入Java版参与讨论
相关主题
solvedRe: how to change the input mode?怎样才能Firefox add-on和java程序之间传递数据
How to wrap line at specific position?GUI问题
如何在TextArea里面画位图question about repaint, update and paint
如何控制文本输入的左界限?比如不准到上一行,不准回到第一个有没有类似MS Visual Studio的Java IDE?
JTextArea做console窗口的目前结果和问题Intellij Idea gui not pure Swing?
keyPressed(KeyEvent e) 需要考虑重入吗getImage有个问题
Re: AWT 和 SWING 在程序应用中有什么区别?jvm是怎么implement monitor的?
JTextArea行间距如何设定?JTextPane怎么快速更新
相关话题的讨论汇总
话题: thread话题: gui话题: 显示话题: 主程序话题: stop