Programming Paradigms

Read Complete Research Material



Programming Paradigms



Programming Paradigms

Task 1

Behaviour of Java Threads

A thread in Java is an instance of the standard Thread class (or a class that extends Thread). The Thread class provides methods to create and control a thread and to synchronise its execution. When a thread object is created (by invoking its constructor using new in the standard way) it remains in the Inactive state until it is activated by calling its start () method . This puts the thread in the Alive state and it becomes Runnable. These signals to the scheduler in the Java virtual machine (JVM) that the thread is ready to run and should be put on the ready queue and scheduled for running. When the JVM is ready to execute the thread, it calls its run() method. This method contains the code that the thread will execute. When the Thread class is extended it is usual to override the inherited run() method to provide the required behaviour of the thread (Jones, 1996).

Note that if the run()method is called directly it is executed by the thread invoking it; a new thread is not created. It is only when run() is invoked via start() that a new thread of control is created and this new thread then executes the run() method. Threads of a particular class can be made to perform different actions by passing data into the thread via a suitably defined constructor which initializes various instance fields in the thread which can then be accessed by the run() method (Knuth, 1997).

The thread remains in the Alive state until its run() method terminates when it moves to the Dead state. However, when a thread has started execution, it may not always be actually running. After a certain time the scheduler in the JVM may schedule another task for execution; it will interrupt the running thread and put it on the ready queue. In the fullness of time, the scheduler will schedule the thread for execution again and it will resume execution of its run() method from the point at which it was interrupted. Alternatively the thread may become Blocked perhaps waiting to acquire a lock or enters a Waiting state by calling the sleep or wait methods of the Thread class (Ungar, 1984). The scheduler allows another thread from the ready queue to run. When the lock is acquired or the thread awakes from its sleep or waits call it is put on the ready queue again and re-enters the Runnable state. Eventually is scheduled by the JVM to run again when it resumes execution from the place that it was blocked. When execution of the run() method is complete the thread enters the Terminated state where it remains until it is garbage collected and finalized after which it ceases to exist.

Example

Below we construct a program which has four threads of control: three threads and the main program which execute independently. Each thread increments its own count variable and outputs the value of this count in a loop, it ...
Related Ads
  • Paradigms - The Business ...
    www.researchomatic.com...

    It is an active behavior in which you challenge your ...

  • Programming Language~
    www.researchomatic.com...

    Programming paradigms are the methods of writ ...

  • Why This Program
    www.researchomatic.com...

    Why I Want To Participate In This Internship Program ...

  • Phd And Edd
    www.researchomatic.com...

    The two major degrees for nursing programs are the P ...

  • Mark 8:34-35
    www.researchomatic.com...

    Matthew 24:29-35 Mark 8:34-35 Rutgers Camden Medical ...