gpt4 book ai didi

java - 如何允许在线程中运行的两个对象在 Java 中进行通信?

转载 作者:行者123 更新时间:2023-12-02 11:42:46 25 4
gpt4 key购买 nike

我将尽力解释这一点,希望您能理解我的问题。

我正在用 Java 设计一个处理器模拟程序,现在我正在编写将控制程序执行的“时钟单元”。基本上,我有一个 ClockUnit 类,它定期在 0 和 1 之间更改状态。我需要第二类Processor才能知道clockunit类何时更改状态,然后执行指令。所以...

  1. ClockUnit 状态 = 0。
  2. 处理器不执行任何操作。
  3. ClockUnit 更改状态 = 1。
  4. 处理器执行指令

目前我正在线程中运行 ClockUnit 类,我现在需要一种方法来运行 Processor 类并允许它不断检查线程的状态时钟,当它变为 1 时执行指令。我不知道该怎么做。

我是否需要创建第二个线程并从第二个线程运行 Processor 类?

我希望大家清楚我需要做什么。在我看来,这是一项非常简单的任务,我只需要一个线程不断检查另一个线程的状态,但我不知道如何去做。

我已经在下面发布了我的代码。实际上并没有太多复杂性。

主类

public class Main {

private static ALU alu;
private static ClockThread clockThread;

public static void main(String[] args)
{
//two threads, both running at the same time, one thread has clock ticking, other thread gets state of ticking clock and executes on rising edge


alu = new ALU();
clockThread = new ClockThread("clockThread", 1);
clockThread.start();

while(clockThread.getClock().getState() == 1)
{
System.out.println("ON");
}


}
}

ClockThread类

import java.util.Timer;

public class ClockThread extends Thread {

private String threadName;
private double instructionsPerSecond;
private Timer timer;
private Clock clockUnit;

public ClockThread(String name, double insPerSec)
{
threadName = name;
System.out.println("Clock thread initialised");
instructionsPerSecond = insPerSec;
}

public void run()
{
clockUnit = new Clock(instructionsPerSecond);
timer = new Timer();
timer.scheduleAtFixedRate(clockUnit, 0, (long) (clockUnit.timePeriod() * 1000));
}

public Clock getClock()
{
return clockUnit;
}
}

时钟类

import java.util.TimerTask;

public class Clock extends TimerTask{

private int state = 0; //the state of the simulation, instrutions will execute on the rising edge;
private double executionSpeed; //in Hz (instructions per second)

private String threadName = "Clock";

public Clock(double instructionsPerSecond)
{
executionSpeed = instructionsPerSecond;
System.out.println("[Clock] Execution speed set to " + executionSpeed + "Hz. (" + timePeriod() + "s per instruction.)");
}

public void run()
{
toggleState();
System.out.println("System State: " + state);
}

public void toggleState()
{
if(state == 1)
{
state = 0;
}
else if(state == 0)
{
state = 1;
}
}

public double timePeriod() //takes the number of instructions per second (hz) and returns the period T (T = 1/f);
{
double period = 1/executionSpeed;
return period;
}

public double getExecutionSpeed()
{
return executionSpeed;
}

public int getState()
{
return state;
}

}

最佳答案

既然您已经有了可靠的时钟源(生产者),您可以使用 BlockingQueue 向 ALU 发送“EdgeChange”警报吗? (负责执行指令的单位)。时钟源将“提供”边沿变化事件,而 ALU?将收到它(并随后进行工作)。以下是对代码的细微更改,以在不同线程中的对象之间共享事件:

主要:

    public static void main(String[] args) {

BlockingQueue<Integer> edgeAlerts = new ArrayBlockingQueue<Integer>(2);
clockThread = new ClockThread("clockThread", 1, edgeAlerts);
clockThread.start();
boolean isInterrupted = false;

while(!isInterrupted) {
try {
Integer edgeValue = edgeAlerts.take();
if (edgeValue == 1) {
System.out.println("Executing instruction");
// Perform the instruction
}
} catch (InterruptedException e) {
isInterrupted = true;
}
}
}

您必须将 BlockingQueue 传递给您的 ClockThread ...

    private final BlockingQueue<Integer> edgeAlerts;

public ClockThread(String name, double insPerSec, BlockingQueue<Integer> edgeAlerts)
{
threadName = name;
this.edgeAlerts = edgeAlerts;
System.out.println("Clock thread initialised");
instructionsPerSecond = insPerSec;
}

还有你的时钟:

private final BlockingQueue<Integer> edgeAlerts;

public Clock(double instructionsPerSecond, BlockingQueue<Integer> edgeAlerts)
{
this.edgeAlerts = edgeAlerts;
executionSpeed = instructionsPerSecond;
System.out.println("[Clock] Execution speed set to " + executionSpeed + "Hz. (" + timePeriod() + "s per instruction.)");
}

你的时钟运行变成:

public void run()
{
toggleState();
System.out.println("System State: " + state);
edgeAlerts.offer(state);
}

请告诉我这是否适合您。

关于java - 如何允许在线程中运行的两个对象在 Java 中进行通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48431515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com