1 / 10

Active Objects (Threads)

Active Objects (Threads). Active vs. Passive Objects. An active object is an object that interacts with its environment, while passive objects only do something when acted upon. A program which contains more than one active object is called a parallel program.

lmargie
Download Presentation

Active Objects (Threads)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Active Objects (Threads)

  2. Active vs. Passive Objects • An active object is an object that interacts with its environment, while passive objects only do something when acted upon. • A program which contains more than one active object is called a parallel program. • A real-time program interacts within its environment under certain time constraints.

  3. Threads • Threads are Java’s implementation of active objects. • When several activities (processes, or threads) are executing simultaneously, what is actually happening is that the system is executing small portions of each program, one at a time, and switching processes rapidly. • So far, all of the programs we have written have had only one thread

  4. Thread Creation • In order to create and activate a new thread in a program use: • Thread a1 = new Thread(); a1.start(); • Thread is defined in java.lang • start() Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. • The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method). • The method run in the Thread class does not do anything, so we must create our own subclasses of Thread & write run

  5. Thread Creation Continued • Because all of our active objects must be subclasses of Thread, we cannot make them subclasses of any other class, so we use: • Thread a2 = new Thread(obj); • This connects the object obj and Thread a2 • The run method in obj will execute, not a2’s run • obj must implement the interface Runnable, which contains the method run. This means that every class that implements Runnable must have a method run.

  6. Objects with Threads • So now, we can define objects that contain an instance variable of class Thread. These classes are said to have their own Thread. • Public Thread activity = new Thread (this); • The this connects the Thread to the current object, which must implement the Runnable interface.

  7. Interruption of Threads • Because most Threads are designed to execute until stopped, you may call the method interrupt to request that the Thread stop itself. • The interruption of a Thread is an exceptional event, so you must put code that might be interrupted in a try statement.

  8. Program Termination • A program terminates when all of its Threads have terminated. • If you do not spawn new Threads, then the program ends when main ends, assuming no abnormal program termination elsewhere. • If you spawn new Threads, make sure that you interrupt them before you end main or they will continue to execute.

  9. Join • The join method waits until a given Thread is finished before moving on. • It can generate an exception, so it must be in at try statement. • You may add time to the call to pause the current Thread for a set time only.

  10. Priority • In a given Java program, you may assign priority to the Threads that are created. • Use getPriority and setPriority • valid priority values: Thread.MIN_PRIORITY, Thread.MAX_PRIORITY, Thread.NORM_PRIORITY ( the default).

More Related