Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software...
-
Upload
felix-hoover -
Category
Documents
-
view
212 -
download
0
Transcript of Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software...
![Page 1: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/1.jpg)
Today’s Agenda
Quick Review
Finish Java Threads
The CS Problem
Advanced Topics in Software Engineering 1
![Page 2: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/2.jpg)
Quick Review
What are the two approaches to creating Java threads?
Advanced Topics in Software Engineering 2
![Page 3: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/3.jpg)
Advanced Topics in Software Engineering 3
Java Threads
Thread Creation
Thread Synchronization
Thread States And Scheduling
Short Demo
![Page 4: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/4.jpg)
Advanced Topics in Software Engineering 4
Thread Creation
There are two ways to create a thread in Java:
Extend the Thread class
Implement the Runnable interface
![Page 5: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/5.jpg)
Advanced Topics in Software Engineering 5
The Thread class
class A extends Thread {public A (String name) { super (name); }public void run () {
System.out.println(“My name is ” + getName());}
}class B {
public static void main (String [] args) {A a = new A (“mud”);a.start ();
}}
![Page 6: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/6.jpg)
Advanced Topics in Software Engineering 6
The Runnable interface
class A extends ... implements Runnable {public void run () {
System.out.println(“My name is ” + getName () );}
}class B {
public static void main (String [] args) {A a = new A ();Thread t = new Thread (a, “mud, too”);t.start ();
}}
![Page 7: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/7.jpg)
Advanced Topics in Software Engineering 7
Java Threads
Thread Creation
Thread Synchronization
Thread States And Scheduling
Short Demo
![Page 8: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/8.jpg)
Advanced Topics in Software Engineering 8
Lock
Each Java object is implicitly associated with a lock.
To invoke a synchronized method of an object, a thread must obtain the lock associated with this object.
The lock is not released until the execution of the method completes.
The locking mechanism ensures that at any given time, at most one thread can execute any synchronized method of an object.
Important: Lock is per object (NOT per method)!
![Page 9: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/9.jpg)
Advanced Topics in Software Engineering 9
wait, notify and notifyAll
The execution of wait on an object causes the current thread to wait until some other thread to call notify or notifyAll.
A thread must own the object lock before it invokes wait on an object. The execution of wait will also release the lock.
When a waiting thread is notified, it has to compete and reacquire the object lock before it continues execution.
What’s the difference between notify and notifyAll?
![Page 10: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/10.jpg)
Advanced Topics in Software Engineering 10
join
A thread t1 can wait until another thread t2 to terminate.
t1 t2
t2.join ()
end
![Page 11: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/11.jpg)
Advanced Topics in Software Engineering 11
interrupt
interrupt allows one thread to send a signal to another thread.
It will set the thread’s interrupt status flag, and will throw a ThreadInterrupted exception if necessary .
The receiver thread can check the status flag or catch the exception, and then take appropriate actions.
![Page 12: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/12.jpg)
Advanced Topics in Software Engineering 12
Other Thread methods
Method sleep puts the running thread into sleep, releasing the CPU.
Method suspend suspends the execution of a thread, which can be resumed later by another thread using method resume.
Method stop ends the execution of a thread.
Note that suspend, resume, and stop has been deprecated in Java 2. (For more info, refer to http://java.sun.com/j2se/1.4.1/docs/guide/misc/threadPrimitiveDeprecation.html.)
![Page 13: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/13.jpg)
Advanced Topics in Software Engineering 13
Daemon Thread
A daemon thread is used to perform some services (e.g. cleanup) for other threads.
Any thread can be marked as a daemon thread using setDaemon (true).
A program terminates when all its non-daemon threads terminate, meaning that daemon threads die when all non-daemon threads die.
![Page 14: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/14.jpg)
Advanced Topics in Software Engineering 14
Java Threads
Thread Creation
Thread Synchronization
Thread States And Scheduling
Short Demo
![Page 15: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/15.jpg)
Advanced Topics in Software Engineering 15
State Transition Diagram
suspendblocked
new runnable running
suspended blocked
dead
start scheduled
CS, yield
IO, sleepwait, join
stop
suspen
d
stop, run ends
susp
end
resu
me
stop
IO completes, sleep expires,
notify, notifyAll,join completes
IO completes
stop
susp
end
stop
resume
![Page 16: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/16.jpg)
Advanced Topics in Software Engineering 16
Scheduling
In general, there are two types of scheduling: non-preemptive scheduling, and preemptive scheduling.
In non-preemptive scheduling, a thread runs until it terminates, stops, blocks, suspends, or yields.
In preemptive scheduling, even if the current thread is still running, a context switch will occur when its time slice is used up.
![Page 17: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/17.jpg)
Advanced Topics in Software Engineering 17
Priorities
Each thread has a priority that also affects their scheduling to run.
If a thread of a higher priority enters the runnable set, the currently running thread will be preempted by the new thread.
![Page 18: Today’s Agenda Quick Review Finish Java Threads The CS Problem Advanced Topics in Software Engineering 1.](https://reader036.fdocuments.us/reader036/viewer/2022083008/56649ed55503460f94be5f40/html5/thumbnails/18.jpg)
Advanced Topics in Software Engineering 18
Java Threads
Thread Creation
Thread Synchronization
Thread States And Scheduling
Short Demo