[英]Implementing Runnable Abstract Class--Multi Threading
I want to call two constructors with different parameters, which in turn run their individual thread but the issue is it only allows one run()
method, is there anyway i can run two run()
method in one class?? 我想用不同的参数调用两个构造函数,它们依次运行它们各自的线程,但问题是它只允许一个
run()
方法,无论如何我可以在一个类中运行两个run()
方法? or any other solution to such situation? 或其他解决方案?
class Threading implements Runnable {
Thread t;
Thread t1;
String name;
Threading(String s) {
name = s;
t = new Thread(this, name);
System.out.println("Constructor 1: " + t);
t.start();
}
public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println("Constructor 1: " + i);
Thread.sleep(2000);
}
} catch (Exception e) {
}
System.out.println("Constructor1 exiting");
}
Threading(int a, int b) {
t1 = new Thread(this, "java2 Thread");
System.out.println("Constructor 2: " + t1);
t1.start();
}
public void run() {
try {
for (int i = 5; i > 0; i++) {
System.out.println("Constructor 2: " + i);
Thread.sleep(3000);
}
} catch (Exception e) {
}
System.out.println("Constructor2 exiting");
}
}
MultipleThread 多线程
class MultipleThread {
public static void main(String[] args) {
new Threading("java thread"); // Constructor 1
new Threading(1, 2); // Constructor 2
try {
for (int i = 5; i > 0; i--) {
System.out.println("Print: " + i);
Thread.sleep(1000);
}
} catch (Exception e) {
}
System.out.println("Exit Main");
}
}
There's no reason to implement your Runnable
with the same Threading
class. 没有理由用相同的
Threading
类实现您的Runnable
。 You can implement them anonymously within each constructor: 您可以在每个构造函数中匿名实现它们:
Threading(String s) {
name=s;
t=new Thread(new Runnable() {
@Override
public void run() {
// ...
}
, name);
System.out.println("Constructor 1: "+t);
t.start();
}
You can have a third class called MyThread
which implement the Runnable
interface and have a constructor with an argument, to save which constructor has created it: 您可以拥有一个名为
MyThread
的第三个类,该类实现Runnable
接口,并具有一个带参数的构造函数,以保存创建它的构造函数:
public class MyThread implements Runnable{
private String calledFrom;
public MyThread(String calledFrom){
this.calledFrom = calledFrom;
}
public void run(){
try {
for (int i = 5; i > 0; i++) {
System.out.println(calledFrom + ": " + i);
Thread.sleep(3000);
}
} catch (Exception e) {
}
System.out.println(calledFrom+" exiting");
}
}
Then change your Threading
class in a way not to implement Runnable interface. 然后以不实现Runnable接口的方式更改
Threading
类。 Now in your both constructors, you can have different instances of MyThread
class with different arguments: "Constructor 1" and "Constructor 2" : 现在,在这两个构造函数中,您可以具有不同参数的
MyThread
类实例: “构造函数1”和“构造函数2” :
class Threading{
...
Threading(String s) {
name = s;
//t = new Thread(this, name);
MyThread mt = new MyThread("Constructor 1");
System.out.println("Constructor 1: " + t);
//t.start();
mt.start();
}
Threading(int a, int b) {
//t1 = new Thread(this, "java2 Thread");
MyThread mt = new MyThread("Constructor 2");
System.out.println("Constructor 2: " + t1);
//t1.start();
mt.start();
}
}
Passing the "Constructor 2" or "Constructor 1" to the MyThread
's constructor is just an example. 将“构造函数2”或“构造函数1”传递给
MyThread
的构造函数只是一个示例。 You can pass some real and useful arguments to MyThread
which helps you to separate the implementation by the type or the value of the arguments. 您可以将一些真实有用的参数传递给
MyThread
,这可以帮助您按参数的类型或值来区分实现。
For example you can have two logic method in the MyThread
class: logicMethodA()
and logicMethodB()
, and based on the value of the argument(s) of the constructor, decide to call which one in the run()
method. 例如,在
MyThread
类中可以有两个逻辑方法: logicMethodA()
和logicMethodB()
,并根据构造函数的参数值决定在run()
方法中调用哪个方法。
Hope this would be helpful. 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.