Multithreaded Java - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Multithreaded Java

Description:

For you to see how Java programs can be made multithreaded through the use of ... Methods that should not be interrupted can be tagged with synchronized modifier ... – PowerPoint PPT presentation

Number of Views:802
Avg rating:3.0/5.0
Slides: 13
Provided by: nicke7
Category:

less

Transcript and Presenter's Notes

Title: Multithreaded Java


1
COMP1681 / SE15Introductionto Programming
Fast Track Session 3
  • Multithreaded Java

2
Todays 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
Session Outline
  • What are threads?
  • Thread class vs. Runnable interface
  • Network servers revisited
  • Dealing with race conditions
  • Scheduled tasks

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
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
Thread vs. Runnable
  • Extend Thread class
  • Override run method with code that needs to be
    executed in a separate thread
  • Cant 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
Writing Servers Revisited
  • Socket alone isnt 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
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_3i
2llsublastore
accountsfrom - amountaccountsto amount
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
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
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
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
Write a Comment
User Comments (0)
About PowerShow.com