110 likes | 134 Views
This article discusses the reuse of threads in distributed software engineering, addressing issues such as thread creation, deallocation, and termination. It proposes a solution using reusable threads and provides an evaluation of its effectiveness.
E N D
Reusing threads Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Some existing techniques • An application needing to deal with reusing threads includes • Data structures • and • Methods • ad-hoc basis to deal with the problem • Plus: addresses a pressing issue • Negative: No software reuse, each application dealing with it will have to come up with its own solution. • Of the two apps reviewed: • Not sufficiently good. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Threading issues to deal with • Expense of thread creation, deallocation. • Reuse of threads • There will be an indirect major issue to deal with as well: • termination of threads being reused. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Thread reuse • Main ideas • A reusable thread can be designed as a running waiting for a job to execute. When the job is given, thread will execute it. • After execution of job, reusable thread is placed on a hold until a next job arrives. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Abstracting Running thread’s Job • We can abstract a job as an instance of Runnable. • For jobs with arguments, we can abstract it as a command object wrapped in a runnable instance interface Command { void execute(); } The user passes a command instance, and the implementation of the execution of the command will do: new Runnable( public void run(){ command.execute(); } ) Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Note on Command • If the command’s execute requires parameters • The parameters to the execute() are given as parameters to the Command’s constructor which set instance variables to be used by execute() • If the command ought to be a query, the implementation of command specifies an instance variable which will contain the final result. The implementation will also provide a query to return such value. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Reusable Thread’s run() • The thread run() method will basically have as body public void run(){ job.run(); } • The questions are: • where will “job” come from. • How to keep the thread’s run() method executing, as we cannot issue start() again on a thread that has been started. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Jobs data structure • For reusable threads, jobs will be coming from a queue of Runnable. • This queue must be designed to be thread-safe. In particular: • When the queue is empty, any thread dequeing will have to be placed on wait. • If the queue has a max length, then any enqueue request will have to be placed on wait. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Body of the run() method public run(){ while(!shutDown()){ (Runnable)jobQueue.dequeue().run(); } } • Note that the condition of the loop is to keep the thread running. • When a thread finishes executing a job’s run() method, and the thread is not shutDown() it will proceed to dequeue a job. If there are no jobs, the thread is put on wait() Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Job creation • The client of the pool thread will proceed to request the pool to execute a job • The execute method of the pool is to add the job to the job queue. • Thus as an object the pool thread will have • A constructor for initialization • The main job of this one will be to start worker threads. • An execute method • A shutdown method that will terminate all those threads on wait() for jobs. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Evaluation • This is a more generic solution, that can be reused. • On the negative side: creating a bunch of threads ahead of time, that may all not be used. • Question: can we design the class so that we only create threads as needed? YES. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads