Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3.

Similar presentations


Presentation on theme: "Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3."— Presentation transcript:

1 Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3

2 SE15 Fast-Track: Multithreaded JavaFT4–2 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

3 SE15 Fast-Track: Multithreaded JavaFT4–3 Session Outline What are threads? Thread class vs. Runnable interface Network servers revisited Dealing with race conditions Scheduled tasks

4 SE15 Fast-Track: Multithreaded JavaFT4–4 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

5 SE15 Fast-Track: Multithreaded JavaFT4–5 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

6 SE15 Fast-Track: Multithreaded JavaFT4–6 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

7 SE15 Fast-Track: Multithreaded JavaFT4–7 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

8 SE15 Fast-Track: Multithreaded JavaFT4–8 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_0 getfield #16 iload_1 dup2 laload iload_3 i2l lsub lastore accounts[from] -= amount; accounts[to] += amount;

9 SE15 Fast-Track: Multithreaded JavaFT4–9 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

10 SE15 Fast-Track: Multithreaded JavaFT4–10 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

11 SE15 Fast-Track: Multithreaded JavaFT4–11 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

12 SE15 Fast-Track: Multithreaded JavaFT4–12 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


Download ppt "Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3."

Similar presentations


Ads by Google