使用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方法将停止正在执行的线程。
分享到:
相关推荐
实现Runnable接口的类必须使用Thread类的实例才能创建线程,通过Runnable接口创建线程分为以下两步
1. 写两个线程,一个线程打印1-52,另一个线程打印字母A-Z。打印顺序为12A34B56C……5152Z。 学习线程之间的通信协调关系。 2. 使用 runnable 接口实现按两... 使用继承Thread类方式和实现Runnable接口两种方式实现。
在Java中创建线程有两种方法:使用Thread类和使用Runnable接口。在使用Runnable接口时需要建立一个Thread实例。因此,无论是通过Thread类还是Runnable接口建立线程,都必须建立Thread类或它的子类的实例。
本篇文章介绍了,java中使用Runnable接口创建线程的方法。需要的朋友参考下
在Java中只支持单继承,因此通过继承Thread类创建线程有一定的局限性,这时可以使用另一种方式,即实现Runnable接口来创建线程。通过这种方式需要在Thread(Runnable target)的构造方法中,传递一个实现了Runnable...
声明自定义线程类实现Runnable接口实现奇数/偶数序列线程并发执行。
大家都知道Runnable和Callable接口都可以作为其他线程执行的任务,但是Runnable接口的run方法没有返回值,而Callable接口的call方法有返回值,那么Callable接口是如何做到的呢?在此我给出一个Demo,看看通过...
线和的应用 runnable接口 runnable接口 runnable接口 runnable接口 runnable接口
一个用java编写的实现Runnable接口的小程序
Eclipse V4.2.0用新建Runnable的方法编写的Android数字时钟应用实例。
thread 线程类 实现runnable接口
1. 什么是Runnable接口: 1.1 介绍Runnable接口 1.2 与Thread类的对比 2. 创建线程的方式: 2.1 继承Thread类 2.2 实现Runnable接口 3. 实现Runnable接口: 3.1 实现步骤 3.2 优点与用途 4. 启动线程: 4.1 使用...
使用Runnable接口创建任务类;使用Runnable接口创建任务类;使用Runnable接口创建任务类;在一个Java程序内启动多条线程;练习;使用Runnable接口创建线程时指定和取得线程名称;The Thread Class ;继承Thread类创建线程类...
1、Java有两种实现多线程的方式:通过Runnable接口、通过Thread直接实现,请掌握这两种实现方式,并编写示例程序。 2、多线程是并发执行的,交替占有cpu执行,请编写示例程序,并观察输出结果。 3、采用线程同步方法...
声明自定义线程类实现Runnable接口实现奇数/偶数序列线程并发执行。
// 使用Runnable接口创建线程 Runnable runnable = new Runnable() { public void run() { // 线程要执行的代码 } }; Thread thread = new Thread(runnable); ``` 2. Java中的抽象类和接口有什么区别? ...
主要介绍了java Runnable接口创建线程的相关资料,需要的朋友可以参考下
实验七 Java多线程 一、实验目的: 熟悉利用Thread类建立多线程方法。 熟悉利用Thread接口建立多线程方法...通过将扩展Thread类建立多线程的方法改为利用Runnable接口的方法,掌握通过Runnable接口建立多线程的方法。
主要为大家详细介绍了java实现Runnable接口适合资源的共享,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
基于JAVA的多线程实现方法-实现Runnable接口 将需要实现多线程的类声明为实现Runnable接口的类,实现run()方法,并将线程体放在该方法里。