在上面的例子中,我们看到线程类(thread)与程序的主类(main)是分隔开的。这样做非常合理,而且易于理解。然而,还有另一种方式也是经常要用到的。尽管它不十分明确,但一般都要更简洁一些(这也解释了它为什么十分流行)。通过将主程序类变成一个线程,这种形式可将主程序类与线程类合并到一起。由于对一个gui程序来说,主程序类必须从frame或applet继承,所以必须用一个接口加入额外的功能。这个接口叫作runnable,其中包含了与thread一致的基本方法。事实上,thread也实现了runnable,它只指出有一个run()方法。
对合并后的程序/线程来说,它的用法不是十分明确。当我们启动程序时,会创建一个runnable(可运行的)对象,但不会自行启动线程。线程的启动必须明确进行。下面这个程序向我们演示了这一点,它再现了counter2的功能:
//: counter3.java
// using the runnable interface to turn the
// main class into a thread.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class counter3
extends applet implements runnable {
private int count = 0;
private boolean runflag = true;
private thread selfthread = null;
private button
onoff = new button("toggle"),
start = new button("start");
private textfield t = new textfield(10);
public void init() {
add(t);
start.addactionlistener(new startl());
add(start);
onoff.addactionlistener(new onoffl());
add(onoff);
}
public void run() {
while (true) {
try {
selfthread.sleep(100);
} catch (interruptedexception e){}
if(runflag)
t.settext(integer.tostring(count++));
}
}
class startl implements actionlistener {
public void actionperformed(actionevent e) {
if(selfthread == null) {
selfthread = new thread(counter3.this);
selfthread.start();
}
}
}
class onoffl implements actionlistener {
public void actionperformed(actionevent e) {
runflag = !runflag;
}
}
public static void main(string[] args) {
counter3 applet = new counter3();
frame aframe = new frame("counter3");
aframe.addwindowlistener(
new windowadapter() {
public void windowclosing(windowevent e) {
system.exit(0);
}
});
aframe.add(applet, borderlayout.center);
aframe.setsize(300,200);
applet.init();
applet.start();
aframe.setvisible(true);
}
} ///:~
现在run()位于类内,但它在init()结束以后仍处在“睡眠”状态。若按下启动按钮,线程便会用多少有些暧昧的表达方式创建(若线程尚不存在):
new thread(counter3.this);
若某样东西有一个runnable接口,实际只是意味着它有一个run()方法,但不存在与之相关的任何特殊东西――它不具有任何天生的线程处理能力,这与那些从thread继承的类是不同的。所以为了从一个runnable对象产生线程,必须单独创建一个线程,并为其传递runnable对象;可为其使用一个特殊的构建器,并令其采用一个runnable作为自己的参数使用。随后便可为那个线程调用start(),如下所示:
selfthread.start();
它的作用是执行常规初始化操作,然后调用run()。
runnable接口最大的一个优点是所有东西都从属于相同的类。若需访问什么东西,只需简单地访问它即可,不需要涉及一个独立的对象。但为这种便利也是要付出代价的――只可为那个特定的对象运行单独一个线程(尽管可创建那种类型的多个对象,或者在不同的类里创建其他对象)。
注意runnable接口本身并不是造成这一限制的罪魁祸首。它是由于runnable与我们的主类合并造成的,因为每个应用只能主类的一个对象。
闽公网安备 35060202000074号