| |
作者:al saganich
使用runnable接口
开发线程应用程序的第二个方法是通过runnable接口来实现。在不少场合,你不能重新定义类的父母,或者不能定义派生的线程类,也许你的类的层次要求你的父类为特定的类。在这些情况下,可以通过runnable接口来实现多线程的功能。
提示:接口是个复杂的技术,要彻底理解它的用法需要花费力气。感兴趣的读者可以阅读我的前一篇文章《接口的阐述》,发表在1998年5月的 visual j++ developer´s journal杂志上。
list c是一个简单的动画小程序,它是一个使用runnable接口的例子。该例子可以放在网页上,它需要从applet类中派生出来。该小程序的目的是通过对一个接一个的图象进行着色,从而显示出动画的效果。因为动画占用了不少处理器时间,我们不打算在图象着色的时候阻塞其他进程的运行。例如,如果打算停止动画,我们不想等到它运行结束时,再调用stop方法。换句话说,我们可以让小程序线程化。
list c: 动画小程序
import java.applet.*; import java.awt.*; public class tstrunnable extends applet implements runnable { private thread m_thread = null; private image m_images[]; private image m_currentimage =null; private int m_nimgwidth = 0; private int m_nimgheight = 0; private boolean m_fallloaded = false; private final int num_images = 18; public tstrunnable() { } private void displayimage(graphics g) { if ( null != m_currentimage ) g.drawimage(m_currentimage,(getsize().width - m_nimgwidth) / 2, (getsize().height - m_nimgheight) / 2, null); } public void paint(graphics g) { if (null != m_currentimage) { rectangle r = g.getclipbounds(); g.clearrect(r.x, r.y, r.width, r.height); displayimage(g); } else g.drawstring("loading images...", 10, 20); } // the applets start method is called when the page is first shown. public void start() { if (m_thread == null) { m_thread = new thread(this); m_thread.start(); } } // the applets stop method is called when the page is hidden. public void stop() { if (m_thread != null) { m_thread.stop(); m_thread = null; } } // the run method is used by the thread // object we created in this start method. public void run() { int iwhichimage = 0; graphics m_graphics = getgraphics(); repaint(); m_graphics = getgraphics(); m_images = newimage[num_images]; mediatracker tracker = new mediatracker(this); string strimage; for (int i = 1; i <= num_images; i++) { m_images[i-1] = getimage(getcodebase(), "img" + new integer(i).tostring() + ".gif"); tracker.addimage(m_images[i-1],0); } try { tracker.waitforall(); m_fallloaded = !tracker.iserrorany(); } catch (interruptedexception e) {} if (!m_fallloaded) { stop(); m_graphics.drawstring("error loading images!", 10, 40); return; } // assuming all images are the same // width and height. //------------------------------------ m_nimgwidth = m_images[0].getwidth(this); m_nimgheight = m_images[0].getheight(this); repaint(); while (true) { try { // draw next image in animation. m_currentimage = m_images[iwhichimage]; displayimage(m_graphics); iwhichimage = (iwhichimage+1) % num_images; thread.sleep(50); } catch (interruptedexception e) { stop(); } } } } 我们使用runnable接口实现了线程,而没有通过创建线程类的派生类的方式。使用runnable接口,需要我们实现run方法。我们也需要创建thread对象的一个实例,它最终是用来调用run方法的。在小程序中的start方法中,我们通过使用thread建构方法产生一个thread对象的实例,其参数就是实现runnable接口的任何类。 thread 对象启动已经定义好的run 方法,而run方法是用来进行动画显示的。当然,从线程类中派生出一个类,并在applet派生类中创建实例,我们可完成同样的事情。该例子是用来演示runnable接口的用法。
在我们接着读下去之前,有几个问题需要回答。你也许会问,浏览器调用java小程序的start和stop方法吗? run 方法是如何被调用的? 情况是这样的,当浏览器启动了一个内部线程时,就相应地启动了applet 的运行。当网页显示时,就启动了applet的start 方法。start方法创建一个线程对象,并把applet自身传送给线程,以实现run方法。
此时,两个线程在运行:由浏览器启动的初始线程,以及处理动画的线程。快速查看applet的start方法,可以知道它创建了线程,并启动了它。类似地,当网页被隐藏后,applet的stop方法就调用了线程的stop方法。
注意:在applets和threads中的 start/stop子程序
在applet 和thread 两个类中都有start和stop方法,但它们的功能不同。一旦applet 显示时,就调用applet的start方法,一旦applet 隐藏时,就调用applet的stop 方法。相反,线程的start方法将调用run方法,线程的stop方法将停止正在执行的线程。
|
|