hsm*_*mit 9 java multithreading java-me
线程通常以两种方式设计(参见java教程):通过扩展Thread类或通过实现Runnable类.无论哪种方式,您都需要指定在线程内运行的内容.
我设计了一个类,一个适用于在线资源的适配器,可以检索不同类型的信息.该类由getInformationOfTypeA()和getInformationOfTypeB()等方法组成.两者都包含连接到在线资源的代码,因此两者都需要进行线程化以避免死锁.
问题是:我该如何设计?我可以像下面这样做,但后来我只能实现一个方法:
public class OnlineResourceAdapter implements Runnable {
public void run() {
//get stuff from resource
getInformationOfTypeA();
}
public static void main(String args[]) {
(new Thread(new OnlineResourceAdapter ())).start();
}
public void getInformationOfTypeA(){
//get information of type A
}
public void getInformationOfTypeB(){
//get information of type B
}
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是为每个方法创建单独的类,但这对我来说似乎不自然.
顺便说一句:我正在j2me中开发我的应用程序
更新:
感谢您的回复,我认为最适合使用以下方法作为方法:
你觉得这怎么样:
public class OnlineResourceAdapter{
public void getInformationOfTypeA(){
Thread t = new Thread(new Runnable() {
public void run() {
//do stuff here
}
});
t.start();
}
public void getInformationOfTypeB(){
Thread t = new Thread(new Runnable() {
public void run() {
//do stuff here
}
});
t.start();
}
}
Run Code Online (Sandbox Code Playgroud)
你觉得这怎么样?
听起来像你应该有两个不同的类:InformationOfTypeAFetcher并且InformationOfTypeBFetcher每个类都应该实现Runnable.他们每个人都可以引用你的OnlineResourceAdapter(或类似的)实例,但如果他们做不同的事情,他们应该是不同的类.
| 归档时间: |
|
| 查看次数: |
20662 次 |
| 最近记录: |