Professional Documents
Culture Documents
1
PROCESSES AND THREADS THREADS
A process is an executing program. It has been allocated • A thread is a single sequential flow of control within a
memory by the operating system. A thread is an program. A thread is also called a lightweight process. A
execution or flow of control in the address space of a thread is similar to a real process in that a thread and a
process; the program counter register points to the next running program are both a single sequential flow of control.
instruction to be executed. A process is a program with However, a thread is considered lightweight because it runs
within the context of a full-blown program and takes
one thread. A process can have more than one thread. advantage of the resources allocated for that program and the
All the threads in a process have their own program program's environment.
counter and their own stack for local (also called
automatic) variables and return addresses of invoked
procedures.
2
CREATING THREADS CREATING THREADS (cont.)
When a thread is created and ready to run, you invoke its In Java, there are two ways to create threaded programs:
start method. The start method spawns a new
thread of control based on the data in the Thread object
That is, you can choose one of two ways to provide a
and then returns. Then the virtual machine invokes the customized run() method for a Java thread:
new thread’s run method, making the thread active.
Subclass the Thread class defined in the java.lang package
When a thread’s run method returns, the thread has and override the run() method.
exited. Provide a class that implements the Runnable interface, also
The standard implementation of Thread.run does defined in the java.lang package. Now, when you instantiate
nothing. You either need to extend Thread to provide a a thread (either directly from the Thread class, or from a
new run method, or create a Runnable object and pass subclass of Thread), give the new thread a handle to an
it to the thread’s constructor. instance of your Runnable class. This Runnable object
provides the run() method to the thread.
3
Applet operation Applet Life Cycle
www Applets do not invoke a main() method as Java
applications do.
2 Instead, applets inherit four special methods associated
3 1
*.html with an applet’s life cycle:
<APPLET
…
init()
/APPLETapplet
4 start()
>
stop()
http://
destroy()
Security restrictions: These methods are invoked by the browser and are
− Byte-code verification not typically called explicitly.
− Limited system access
− No access to local file system
− Limited network operations
− ...
where to draw it
4
The Producer and Consumer Problem The Producer and Consumer Problem
The Producer generates an integer between 0 and 9 Main Program
(inclusive), stores it in a "CubbyHole" object, and prints
the generated number. Producer
The Consumer consumes all integers from the CubbyHole
(the exact same object into which the Producer put the
integers in the first place) as quickly as they become Consumer
available.
To prevent race conditions, the storage of a new integer CubbyHole
into the CubbyHole by the Producer must be
synchronized with the retrieval of an integer from the
CubbyHole by the Consumer.
The Java language and runtime system support thread
synchronization through the use of monitors
Synchronization MONITORS
Conflicts may arise when more than one thread are inside In general, a monitor is associated with a specific data item
an object! (a condition variable) and functions as a lock on that
data. When a thread holds the monitor for some data
class Buffer item, other threads are locked out and cannot inspect or
modify the data.
{
private int [100] buf; The code segment within a program that must be accessed
by concurrent threads in an exclusive manner is called a
private int index=0;
critical section. In Java, you mark critical sections in
public void processing (int item) { your program with the synchronized keyword.
buf[index] = item;
index++;
}
}
5
Java Monitors Are Re-entrant Inter-Thread Communication
Java monitors are re-entrant so as to prevent the Often one thread has to wait for another thread to do
possibility of a single thread deadlocking itself on a something before it can perform its task.
monitor that it already holds.
class Reentrant {
public synchronized void a() { The threads need to communicate between each other
b(); when they have completed their task.
System.out.println("here I am, in
a()");
}
public synchronized void b() {
System.out.println("here I am, in
b()");
}
}
Output:
here I am, in b()
here I am, in a()
The notify() and wait() methods The notify() and wait() methods (cont.)
notify( ) The Object class contains two other versions of the
Notifies a thread in a wait state that the condition it is waiting wait() method:
for has been achieved. wait(long timeout)
notifyAll( ) waits for notification or until the timeout period has elapsed--
Performs similarly to the notify() method except that all the timeout is measured in milliseconds.
waiting threads are awakened. wait(long timeout, int nanos)
wait( ) waits for notification or until timeout milliseconds plus nanos
Causes the invoking thread to wait for a condition to occur. nanoseconds have elapsed.
6
Abstract classes Example: abstractAreas.java
You can create an abstract class when you want to abstract class Shape {
double d1,d2; class rectangle1 extends Shape {
manipulate a set of classes through this common
interface. rectangle1(double a,double b) {
Shape(double a,double b) { super(a,b); }
d1=a; double area() {
A class containing abstract methods is called an abstract d2=b;
System.out.println("area for rectangle:");
class. If a class contains one or more abstract methods, }
return d1*d2; }
the class must be qualified as abstract. abstract double area();
}
}
If you inherit from an abstract class and you want to make class triangle1 extends Shape {
objects of the new type, you must provide method triangle1(double a,double b) {
definitions for all the abstract methods in the base class. super(a,b); }
double area() {
System.out.println("area for triangle:");
return d1*d2/2; }
}
7
Multiple inheritance in Java Example
[public] interface interfaceName [extends interface1, interface2,…] interface CanFight {
{ //interface body} void fight();}
interface CanFly {
[modifiers] class className [extends superClass] implements void fly();}
interface [, interface]
class ActionCharacter {
class triangle extends Mygraphics implements Brushes{ public void fight() { }
…
…. //each methods declared in Brushes must be implemented class Hero extends ActionCharacter
here implements CanFight, CanSwim, CanFly {
…. public void swim() { }
} public void fly() { }
}
Spring, 2003 Java 43 Spring, 2003 Java 44
Example
public class Adventure {
static void t(CanFight x) { x.fight(); }
static void u(CanSwim x) { x.swim(); }
static void v(CanFly x) { x.fly(); }
static void w(ActionCharacter x) { x.fight(); }