Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Reusing threads.

Similar presentations


Presentation on theme: "Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Reusing threads."— Presentation transcript:

1 Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Reusing threads

2 Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 2 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.

3 Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 3 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.

4 Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 4 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.

5 Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 5 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(); } )

6 Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 6 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.

7 Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 7 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.

8 Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 8 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.

9 Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 9 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()

10 Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 10 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.

11 Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 11 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.


Download ppt "Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Reusing threads."

Similar presentations


Ads by Google