java - 使用具有多个对象的单个线程?
问题描述
所以假设我有一个实现 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);
如何启动单个线程并知道何时d1
或d2
正在运行?
如果我运行:
Thread t1 = new Thread(d1);
Thread t2 = new Thread(d2);
t1.start();
t2.start();
然后在这里我创建两个线程而不是一个。
解决方案
推荐阅读
- vba - 打印最左边的字符
- html - 使用表单组为 HTML 设置选择值
- python - 运行迁移时出错返回 Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: near ")": 语法错误
- java - Libgdx 将运动图像返回到原始位置
- python - 在日期 odoo 之后从其他字段更新字段
- python - 素数总和
- android - Android MediaPlayer.create 失败
- selenium-webdriver - 使用 selenium 和 c# 从 Combobox 下拉列表中选择值
- python - SQL 游标重构为 Spark 集群上的 Python 状态机
- jquery - 在 ChartJS 中突出显示最后点击的栏