简体   繁体   中英

Simulating multi-core CPU using thread concurrency in Java

I'm trying to write a program that simulates a multi-core cpu.

We have 3 threads that will do the job of cores for us.

There are also a few tasks that will be done as a FIFO(first in first out) order. Since I'm new to concept of threads in java, after putting a lot of thinking on this I am still clueless on how to write the program.

I just want to know how I can do this the simplest way and what methods and classes must be used.

I'm thinking about using wait() , notifyAll() things, but I'm not sure this will work or not.

In this program it's like in every step the main method will print out and increment the master clock then the threads will each decide whether they must print something or not(start a new task, context switch) according to the current value of the master clock. I will appreciate any hint.

The output must look like something like this:

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

From your description, it appears you don't need (or should want) Java threads for this simulation. The virtual cores are running in lockstep based on the master clock. All you need is a main loop that represents the master clock, and an operation queue for each virtual core. At each master clock step you determine each virtual core's action and perform it. No real threads needed.

You definitely should learn more about concurrency in Java. This is an article about thread pools and working queues which can help you to understand what you can do in Java - http://www.ibm.com/developerworks/library/j-jtp0730/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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