CSC 143 1 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

CSC 143 1

Description:

Threads CSC 143 – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 13
Provided by: Franco105
Category:
Tags: csc | buffer | method

less

Transcript and Presenter's Notes

Title: CSC 143 1


1
CSC 143
  • Threads

2
Introducing Threads
  • A thread is a flow of control within a program
  • A piece of code that runs on its own. The
    execution environment is the same as for the rest
    of the program. The thread can share data with
    other parts (other threads) of the program.
  • A thread executes concurrently with the other
    threads of the program.
  • e.g. when painting (or repainting), the method
    paint is executed by a thread.
  • Key issue synchronization
  • If the threads run concurrently and share data,
    what happens when one thread uses data modified
    at the same time by another thread?

3
The Thread class
  • in the package java.lang
  • Define a thread by extending the Thread class
  • override the run method to specify what the
    thread will do
  • public class MyThread extends Thread
  • public void run()
  • / code for thread to run when it
  • gets control /
  • // more code
  • to make a thread, write
  • MyThread t new MyThread()
  • t.start()

4
Life of a thread
  • When start is called, the thread t becomes ready
    to be executed by the processor.
  • When another thread gets a turn, t will stop
    executing its run method. It will start again
    from where it left off, when it gets another turn
  • The scheduling of the threads is the
    responsibility of the OS
  • However, we can temporarily stop the execution,
    e.g. using the methods sleep and wait (see the
    examples to come)

5
Example
  • Create 2 threads that write their names and sleep
  • see the full code on the class web site
    ThreadName.java
  • Can you predict the order of execution?

6
Using the Runnable interface
  • If our class already extends another class, how
    do we make it a thread?
  • public class D extends B
  • Use the Runnable interface and implement the run
    method
  • public class D extends B implements Runnable
  • //Thread to host our run method
  • private Thread t
  • // implement run
  • public void run()/code/
  • // to start the thread
  • public D()
  • t new Thread(this)
  • t.start()
  • How would you program the example ThreadName
    using Runnable?

7
Concurrent threads
  • When 2 threads running independently can modify
    the same data, they need to cooperate.
  • If not, the state of the data is unpredictable
  • See an example BadConcurrency.java on the class
    web site.
  • To avoid the problem, synchronize the two
    threads,e.g. in BadConcurrency.java write
  • synchronized(data)
  • data0val
  • data1val
  • checkData()
  • // The thread must have an exclusive
  • // access to data before entering the
  • // synchronized block

8
synchronization
  • Use the keyword synchronized
  • What can be synchronized?
  • A block of Java code (as in the previous
    example).
  • synchronized(myObject)/block/
  • The thread needs a lock on myObject before
    entering the block.
  • An instance method, e.g.
  • public synchronized void update()
  • /code/
  • a thread can execute update only if it has a lock
    on the class object (this).

9
wait method
  • available for any object since it is part of the
    Object class
  • inside of a synchronized block, a thread can give
    up its hold on a lock by calling wait. Then,
    another thread can take the lock.
  • When the first thread reacquires its lock, it
    starts from where it left off.
  • Reacquisition is not assured. The thread must
    wake up. This is done if another thread calls
    notify() or notifyAll(). Then the thread is
    requeued and competes for the lock with the other
    threads.

10
Example
  • A Sender sends items to a Receiver via a Buffer
  • Both the Sender and the Receiver can modify the
    Buffer content.
  • The access to the Buffer must be synchronized if
    Sender and Receiver are two independent threads.
  • See TestSenderReceiver.java (and other files) on
    the class web site.

11
deadlock
  • When threads wait for locks that can't be freed.
  • e.g. in the Sender-Receiver example, there is
    deadlock if
  • The Sender sends only to an empty buffer
  • The Receiver takes only from a full buffer

12
Animation
  • Animation is better achieved with threads
  • Without an animation thread, one part of the
    program can hold up the animation part.
Write a Comment
User Comments (0)
About PowerShow.com