100 likes | 120 Vues
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.
E N D
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.
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
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
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.
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.
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.
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.
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.
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).