120 likes | 200 Views
COMP1681 / SE15 Introduction to Programming. Multithreaded Java. Fast Track Session 3. Today’s Learning Objectives. For you to see how Java programs can be made multithreaded through the use of Thread objects & the Runnable interface
E N D
COMP1681 / SE15Introductionto Programming Multithreaded Java Fast Track Session 3
Today’s Learning Objectives • For you to see how Java programs can be made multithreaded through the use of Thread objects & the Runnable interface • For you to appreciate the problems that can occur when threads interfere with one another… • …and for you to understand how to fix those problems SE15 Fast-Track: Multithreaded Java
Session Outline • What are threads? • Thread class vs. Runnable interface • Network servers revisited • Dealing with race conditions • Scheduled tasks SE15 Fast-Track: Multithreaded Java
What Are Threads? • A multitasking OS time-slices between processes to give the illusion of concurrency • Threads are a finer-grained concept allowing concurrent tasks within a single process • Much easier to share data between threads, because they operate in the same address space SE15 Fast-Track: Multithreaded Java
Threads in Java • Basic features in java.lang • Thread class • Runnable interface • Additional features in java.util • Timer & TimerTask (since J2SE 1.3) • Advanced features (J2SE 5.0) • Thread pools • Thread-aware collections • Locks, semaphores, etc SE15 Fast-Track: Multithreaded Java
Thread vs. Runnable • Extend Thread class • Override run method with code that needs to be executed in a separate thread • Can’t do this if the code is in a class that already has a superclass (and it may not make sense, anyway…) • Implement Runnable interface • Forces us to write a run method, containing code that needs to be executed in a separate thread • Need to create separate Thread objects to operate on our Runnable objects SE15 Fast-Track: Multithreaded Java
Writing Servers Revisited • Socket alone isn’t sufficient to write a server • We need something to ‘sit by the phone, waiting for incoming calls’… • Java provides ServerSocket class to • Listen on a particular port • Negotiate connection with client • Open a Socket connection between hosts • Spawn thread to handle communication with client SE15 Fast-Track: Multithreaded Java
Race Conditions • What happens if two threads have access to the same object and each tries to modify object state? • Outcome depends on how the instructions executed by the two threads are interleaved • Example: bank transfers aload_0getfield #16iload_1dup2laloadiload_3i2llsublastore accounts[from] -= amount;accounts[to] += amount; SE15 Fast-Track: Multithreaded Java
Synchronization • Methods that should not be interrupted can be tagged with synchronized modifier • When a synchronized method is called by a thread, a mutex lock on the object is acquired • The lock prevents other threads from calling any synchronized method on that object… • …until the first thread exits its synchronized method SE15 Fast-Track: Multithreaded Java
wait & notify • wait method allows a thread to wait within a synchronized method and relinquish the object lock • Waiting thread goes onto ‘wait list’ and will be ignored by thread scheduler • For a thread to be removed from wait list, another thread must invoke notify or notifyAll on same object • notifyAll method unblocks all waiting threads, freeing them to compete for the object lock after current thread has exited the synchronized method SE15 Fast-Track: Multithreaded Java
Scheduled Tasks • Implement task to be scheduled as a class that extends abstract class TimerTask • Override run method with code be executed • Timer class has methods to execute TimerTask code at a later time, once only or repeatedly • Scheduled tasks are placed in an ordered queue and are executed sequentially by a single thread • Tasks should be short-lived SE15 Fast-Track: Multithreaded Java
Summary • We have • Seen how Java provides the Thread class and Runnable interface to support multithreaded code • Considered how a simple network server can be made multithreaded • Examined how race conditions can arise with threads, and how synchronization can help resolve problems • Looked at how tasks can be scheduled to run at regular intervals in Java programs SE15 Fast-Track: Multithreaded Java