Unformatted text preview:

Today’s AgendaQuick ReviewJava ThreadsThread CreationThe Thread classThe Runnable interfaceSlide 7Lockwait, notify and notifyAlljoininterruptOther Thread methodsDaemon ThreadSlide 14State Transition DiagramSchedulingPrioritiesSlide 18Today’s AgendaQuick ReviewFinish Java ThreadsThe CS ProblemAdvanced Topics in Software Engineering 1Quick ReviewWhat are the two approaches to creating Java threads?Advanced Topics in Software Engineering 2Advanced Topics in Software Engineering 3Java Threads Thread Creation Thread Synchronization Thread States And Scheduling Short DemoAdvanced Topics in Software Engineering 4Thread CreationThere are two ways to create a thread in Java: Extend the Thread class Implement the Runnable interfaceAdvanced Topics in Software Engineering 5The Thread classclass 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 ();}}Advanced Topics in Software Engineering 6The Runnable interfaceclass 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 ();}}Advanced Topics in Software Engineering 7Java Threads Thread Creation Thread Synchronization Thread States And Scheduling Short DemoAdvanced Topics in Software Engineering 8LockEach 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)!Advanced Topics in Software Engineering 9wait, notify and notifyAllThe 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?Advanced Topics in Software Engineering 10joinA thread t1 can wait until another thread t2 to terminate. t1t2t2.join ()endAdvanced Topics in Software Engineering 11interruptinterrupt 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.Advanced Topics in Software Engineering 12Other Thread methodsMethod 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.)Advanced Topics in Software Engineering 13Daemon ThreadA 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.Advanced Topics in Software Engineering 14Java Threads Thread Creation Thread Synchronization Thread States And Scheduling Short DemoAdvanced Topics in Software Engineering 15State Transition Diagramsuspendblockednew runnable runningsuspended blockeddeadstart scheduledCS, yieldIO, sleepwait, joinstopsuspendstop, run endssuspendresumestopIO completes, sleep expires,notify, notifyAll,join completesIO completesstopsuspendstopresumeAdvanced Topics in Software Engineering 16SchedulingIn 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.Advanced Topics in Software Engineering 17PrioritiesEach 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.Advanced Topics in Software Engineering 18Java Threads Thread Creation Thread Synchronization Thread States And Scheduling Short


View Full Document

UT Arlington CSE 6324 - Java threads

Download Java threads
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java threads and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java threads 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?