繁体   English   中英

在Java中使用线程并发模拟多核CPU

[英]Simulating multi-core CPU using thread concurrency in Java

我正在尝试编写一个模拟多核cpu的程序。

我们有3个线程可以为我们完成核心工作。

还有一些任务将作为FIFO(先进先出)顺序完成。 由于我对java中的线程概念不熟悉,在对此进行了大量思考之后,我仍然对如何编写程序一无所知。

我只想知道如何以最简单的方式做到这一点以及必须使用哪些方法和类。

我正在考虑使用wait()notifyAll()东西,但我不确定这是否会起作用。

在这个程序中,就像在每个步骤中主方法将打印出来并递增主时钟然后线程将根据主时钟的当前值决定是否必须打印某些内容(启动新任务,上下文切换) 。 我会感激任何提示。

输出必须如下所示:

Task 2 : 6 time units
Task 3 : 9 time units
Task 4 : 10 time units
Task 5 : 10 time units
Task 6 : 8 time units
Task 7 : 7 time units

--------------------

Master Clock : 0
  Core 2 started its first task of 7 time units
  Core 0 started its first task of 9 time units
  Core 1 started its first task of 6 time units
Master Clock : 1
Master Clock : 2
Master Clock : 3
Master Clock : 4
Master Clock : 5
Master Clock : 6
  Core 1 started context switch
Master Clock : 7
  Core 2 started context switch
Master Clock : 8
  Core 1 started a new task of 9 time units
Master Clock : 9
  Core 2 started a new task of 10 time units
  Core 0 started context switch
Master Clock : 10
Master Clock : 11
  Core 0 started a new task of 10 time units
Master Clock : 12
Master Clock : 13
Master Clock : 14
Master Clock : 15
Master Clock : 16
Master Clock : 17
  Core 1 started context switch
Master Clock : 18
Master Clock : 19
  Core 1 started a new task of 8 time units
  Core 2 started context switch
Master Clock : 20
Master Clock : 21
  Core 0 completed a total of 2 tasks
  Core 2 started a new task of 7 time units
Master Clock : 22
Master Clock : 23
Master Clock : 24
Master Clock : 25
Master Clock : 26
Master Clock : 27
  Core 1 completed a total of 3 tasks
Master Clock : 28
  Core 2 completed a total of 3 tasks

根据您的描述,您似乎不需要(或不应该)使用Java线程进行此模拟。 虚拟内核基于主时钟以锁步方式运行。 您只需要一个代表主时钟的主循环,以及每个虚拟内核的操作队列。 在每个主时钟步骤中,您可以确定每个虚拟核心的操作并执行它。 不需要真正的线程。

你肯定应该学习更多关于Java的并发性。 这是一篇关于线程池和工作队列的文章,可以帮助您了解Java中可以做什么 - http://www.ibm.com/developerworks/library/j-jtp0730/index.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM