Java 如何创建线程来控制另一个线程?
我尝试制作简单的多线程机器人。他把头当作线,两条腿,一条腿,一根线。一切都很好,但是如何创建控制线程来控制每个分支呢 我的代码是:Java 如何创建线程来控制另一个线程?,java,multithreading,Java,Multithreading,我尝试制作简单的多线程机器人。他把头当作线,两条腿,一条腿,一根线。一切都很好,但是如何创建控制线程来控制每个分支呢 我的代码是: public class Controller implements CommandInterface{ private final Object monitor = new Object(); private int numOfSteps = 10; class Legs implements Runnable { private fin
public class Controller implements CommandInterface{
private final Object monitor = new Object();
private int numOfSteps = 10;
class Legs implements Runnable {
private final String whichLeg;
Legs(String whichLeg) {
this.whichLeg = whichLeg;
}
@Override
public void run() {
for(int i = 0; i < numOfSteps;i++) {
synchronized (monitor) {
step();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
monitor.notify();
try {
monitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void step() {
System.out.println(whichLeg);
}
}
Legs left = new Legs("left feet");
Legs right = new Legs("right feet");
@Override
public void execute() {
new Thread(left).start();
new Thread(right).start();
}
}
我知道我应该使用join()
等待另一个线程。
我希望看到这样的结果:
Init头
头发1步
左脚
头发2步
右脚
等等…
我尝试在main方法中创建头线程,并调用
join()
,但它等待当前线程,但我需要等待分支。我尝试创建头线程,如thread head=new thread(new Runnable{tryed here to run execute method})代码>但这一切都不起作用。有很多可能的选择。以下是其中之一:
每条腿本身就是一个监视器。当头部线程启动时,它开始通知/等待腿部。因此,在第一次迭代中,头部将通知/等待第一段,在第二次迭代中,头部将通知/等待第二段,依此类推,直到结束。腿只是永远在循环,等待头部线程的通知。他们的工作是接收通知、打印适当的消息并向负责人发送通知。
这是草图:
interface CommandInterface {
void execute();
}
class Controller implements CommandInterface {
private static final int NUM_OF_STEPS = 10;
private final Legs[] legs = {
new Legs("left feet"),
new Legs("right feet")
};
@Override
public void execute() {
Executors.newSingleThreadExecutor().execute(() -> {
System.out.println("Init head");
for (Legs leg : legs) {
leg.start();
}
for (int i = 0; i < NUM_OF_STEPS; i++) {
try {
TimeUnit.SECONDS.sleep(1);
int currentLegIndex = i % legs.length;
synchronized (legs[currentLegIndex]) {
System.out.println("head sends make " + (i + 1) + " step");
legs[currentLegIndex].notify();
legs[currentLegIndex].wait();
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted!", e);
}
}
});
}
class Legs extends Thread {
private final String whichLeg;
Legs(String whichLeg) {
this.whichLeg = whichLeg;
}
@Override
public void run() {
while (true) {
try {
synchronized (this) {
this.wait();
step();
this.notify();
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted!", e);
}
}
}
private void step() {
System.out.println(whichLeg);
}
}
}
class ClientInterface {
public static void main(String [] args) {
new Controller().execute();
}
}
接口命令接口{
void execute();
}
类控制器实现CommandInterface{
私有静态final int NUM OF_STEPS=10;
私人最后航段[]航段={
新腿(“左脚”),
新腿(“右脚”)
};
@凌驾
public void execute(){
Executors.newSingleThreadExecutor().execute(()->{
系统输出打印项次(“初始头”);
用于(腿:腿){
leg.start();
}
for(int i=0;i
您也可以考虑创建共享<代码> CurtDutLabCH < /C>。我当然会推荐阅读它的javadoc。我想你会理解这个想法,自己创造一个更优雅的解决方案;)
interface CommandInterface {
void execute();
}
class Controller implements CommandInterface {
private static final int NUM_OF_STEPS = 10;
private final Legs[] legs = {
new Legs("left feet"),
new Legs("right feet")
};
@Override
public void execute() {
Executors.newSingleThreadExecutor().execute(() -> {
System.out.println("Init head");
for (Legs leg : legs) {
leg.start();
}
for (int i = 0; i < NUM_OF_STEPS; i++) {
try {
TimeUnit.SECONDS.sleep(1);
int currentLegIndex = i % legs.length;
synchronized (legs[currentLegIndex]) {
System.out.println("head sends make " + (i + 1) + " step");
legs[currentLegIndex].notify();
legs[currentLegIndex].wait();
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted!", e);
}
}
});
}
class Legs extends Thread {
private final String whichLeg;
Legs(String whichLeg) {
this.whichLeg = whichLeg;
}
@Override
public void run() {
while (true) {
try {
synchronized (this) {
this.wait();
step();
this.notify();
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted!", e);
}
}
}
private void step() {
System.out.println(whichLeg);
}
}
}
class ClientInterface {
public static void main(String [] args) {
new Controller().execute();
}
}