如何在另一个线程中执行此功能? [英] How execute this function in another thread?

查看:133
本文介绍了如何在另一个线程中执行此功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void LoadImage()
    {
    repeat = 4;
    for (int i = 0; i < repeat; i++)
    {
    try{
        if (is.available() != 0)
                 {
            repeat +=2;
            slen = is.available();
            isize += slen;
            try{
            is.read(img, offset, slen);
            im = Image.createImage(img,0,isize);
            g.drawImage(im, 0, 0, Graphics.LEFT | Graphics.TOP);
            flushGraphics();
            offset = isize;
                     }
            catch(Exception cc){
                g.drawString(cc.toString(), 0, 0, 20);
                flushGraphics();
            }
        }
    }
    catch(Exception ex){
    }

            try{
            Thread.sleep(140);
            }
            catch(Exception xx){}

    }
}//LoadImg


我看过几个例子,那里说需要创建一个类,但是如何访问从一个类到另一个类的变量呢?还是我听不懂..
请帮忙.


I seen few examples, there says that need create a Class, but how to access to variables, from one class to another? Or I don''t understand something..
Please, help.

推荐答案

为此,您可以使用SwingUtilities invokeLater()invokeAndWait()方法.诀窍是,您可以在线程中进行所有加载和绘制,但是只能从UI线程本身完成到用户界面上的绘制.该链接将为您提供更多信息和示例:
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.htm [ ^ ]

这是另一个使用示例的链接,该示例甚至使用了另一种方法.该线程正在使用SwingWorker.publish方法将值传递给process方法(该方法将接收传递的值并进行gui更新).您还可以下载一个示例,它可能会帮助您入门.

http://download.oracle.com/javase/tutorial/uiswing/concurrency/interim.html [^ ]

示例下载:
http://download.oracle.com/javase/tutorial/uiswing/examples/concurrency/index.html#Flipper [ ^ ]

祝你好运!
For this you could use use the SwingUtilities invokeLater() or invokeAndWait() method. The trick is that you can do all the loading and drawing in a thread but drawing onto the user interface can only be done from the UI thread itself. The link will give you more info and examples:
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.htm[^]

Here another link with an example that uses even another method. The thread is using the SwingWorker.publish method to pass values to the process method (that receives the values passed and does the gui updating). There is also an example you can download and might help you get started.

http://download.oracle.com/javase/tutorial/uiswing/concurrency/interim.html[^]

Example download:
http://download.oracle.com/javase/tutorial/uiswing/examples/concurrency/index.html#Flipper[^]

Good luck!


这篇关于如何在另一个线程中执行此功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆