对多个对象使用单个线程?

问题描述

假设我有一个实现 Runnable 接口的类:

class Doctor implements Runnable{
    private String name;
    private int id;
    
    Doctor(String name,int id){
        this.name = name;
        this.id = id;
    }
    
    public void run(){ /* some code */}
}

现在Doctor类作为一个线程工作,但是如何从多个对象中获得一个Doctor类的线程?

例如:

Doctor d1 = new Doctor("George",1);
Doctor d2 = new Doctor("Jim",2);

如何启动单个线程并知道 d1d2 何时运行?

如果我跑:

Thread t1 = new Thread(d1);
Thread t2 = new Thread(d2);
t1.start();
t2.start();

然后在这里我创建了两个线程而不是一个

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)