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

时间:2018-01-24 21:01:34

标签: java multithreading concurrency

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

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

  1. ClockUnit州= 0。
  2. Processor什么也没做。
  3. ClockUnit更改状态= 1。
  4. Processor执行指令
  5. 目前我在一个线程中运行ClockUnit类,我现在需要一种运行Processor类的方法,并允许它不断检查时钟的状态以及何时更改为a 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;
        }
    
    }
    

1 个答案:

答案 0 :(得分:1)

由于您已拥有可靠的时钟源(制作人),因此您可以使用BlockingQueue发送' EdgeChange'提醒ALU? (负责执行指令的单位)。时钟源将提供'边缘变化事件和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);
    }

请告诉我这是否适合您。