开发者

writing a thread(educational purpose)

开发者 https://www.devze.com 2023-02-09 14:36 出处:网络
Sorry if thi开发者_JAVA技巧s is aduplicate... I have a task to write a thread. And the question is - what a good thread class should contain. I looked through Java implementation and some other, but s

Sorry if thi开发者_JAVA技巧s is a duplicate... I have a task to write a thread. And the question is - what a good thread class should contain. I looked through Java implementation and some other, but since it is just an educational project, I wouldn't want to make it too complex. If you can tell or point me to source witch contains required information, I would be very grateful.


Simple thread class consists of following along with threadManager class for easier management of multiple threads

Thread class:

Constructor

function to execute thread

Check if thread is running and process thread's output, if any is present. Returns TRUE if the thread is still executing, FALSE if it's finished.

Wait until the thread exits

ThreadManager class:

Constructor

Add an existing thread to the manager queue.

Remove a thread from the manager queues.

Process all threads. Returns the number of threads that are still running.

Create and start a new thread. Returns the ID assigned to the thread or FALSE on error.

Remove a finished thread from the internal queue and return it. Returns FALSE if there are no threads that have completed execution.


On the highest level of abstraction you can think about the thread as a combinration of:

  1. Finite-state machine to represent thread's state

  2. Queue of tasks to proceed

  3. Scheduler which can manage threads (start, pause, notify etc ..). Scheduler can be OS level scheduler or some custom scheduler, for example, on the VM level - so called "green threads".

To be more specific, I would recommend to look at Erlang VM. Sources are available online and you can go through their implementantion for "green threads" which are extremely lightweight.

Erlang Downloads

0

精彩评论

暂无评论...
验证码 换一张
取 消