Docsity

Java Threads and Synchronization: A Comprehensive Guide, Slides of Java Programming

Toggle sidebar
bg1
Threads and Synchronisation in Java
CalinCur escu
Threads and Synchronisation in Java
Calin Curescu
reusing from Tommy Olssons TDDB32 slides
9 pages
TDDC32 lecture 1, 2006
(doings several things at once)
Threads and Synchronisation in Java
CalinCur escu
2of 9
TDDC32 lecture 1, 2006
What is a Thread?
•Thread of execution in a program
–Follows the statments of the program
–Branches when meeting if-clauses
–Loops in while-clauses
•Operating systems multitasking:
–Several processes (or threads) execute in parallel and
compete for the CPU
–A process has its own memory space
–A thread shares a the memory space with other threads
• Several threads inside a process
• Have access to the same data
–The process here is the Java Virtual Machine (JVM)
Threads and Synchronisation in Java
CalinCur escu
3of 9
TDDC32 lecture 1, 2006
The class Thread
• Implements interface Runnable with method run()
• Any class extending Thread can start a new execution thread
• Methods in Thread:
–run()– the method that will be executed in a new execution thread
–start()– starts new thread’s execution and calls run()
–join()– wait for the thread to die (to finish run())
– interrupt() – interrupts the thread
– isAlive () - Tests if the thread is alive (has been started and not yet
dead)
• Constructors:
–Thread()– creates a new thread, method run must be overriden
– Thread(Runnable) – mehod run() taken from Runnable object
• Static methods in Thread:
– sleep(long) – makes current thread sleep for given ms
– yield( ) – makes current thread give up the CPU
– currentThread() – returns the currently running thread object
Threads and Synchronisation in Java
CalinCur escu
4of 9
TDDC32 lecture 1, 2006
Thread state
initial
running
ready
blocked
dead
wait()
notify()
yield()
end of run()
scheduler scheduler
start()
•wait(), notify() and join() used for synchronisation
– we’ll come back in a couple of slides
join()
sleep()
Threads and Synchronisation in Java
CalinCur escu
5of 9
TDDC32 lecture 1, 2006
Scheduling
•How do two threads execute on
–2 processors?
–1 processor?
•Round robin with time share
–Usual systems
•Priority based
–Real-time systems
Threads and Synchronisation in Java
CalinCur escu
6of 9
TDDC32 lecture 1, 2006
Blocking and synchronisation
•Blocking
–Waiting for external or internal events
•Critical section
pf2

Partial preview of the text

Download Java Threads and Synchronization: A Comprehensive Guide and more Slides Java Programming in PDF only on Docsity!

Threads and Synchronisation in JavaCalin Curescu

Threads and Synchronisation in Java

Calin Curescu

reusing from Tommy Olssons TDDB32 slides

9 pages TDDC32 lecture 1, 2006

(doings several things at once)

Threads and Synchronisation in JavaCalin Curescu 2 of 9 TDDC32 lecture 1, 2006

What is a Thread?

  • Thread of execution in a program
    • Follows the statments of the program
    • Branches when meeting if-clauses
    • Loops in while-clauses
  • Operating systems multitasking:
    • Several processes (or threads) execute in parallel and compete for the CPU
    • A process has its own memory space
    • A thread shares a the memory space with other threads
      • Several threads inside a process
      • Have access to the same data
    • The process here is the Java Virtual Machine (JVM)

Threads and Synchronisation in Java Calin Curescu TDDC32 lecture 1, 20063 of 9

The class Thread

  • Implements interface Runnable with method run()
  • Any class extending Thread can start a new execution thread
  • Methods in Thread:
    • run() – the method that will be executed in a new execution thread
    • start() – starts new thread’s execution and calls run()
    • join() – wait for the thread to die (to finish run())
    • interrupt() – interrupts the thread
    • isAlive() - Tests if the thread is alive (has been started and not yet dead)
  • Constructors:
    • Thread() – creates a new thread, method run must be overriden
    • Thread(Runnable) – mehod run() taken from Runnable object
  • Static methods in Thread:
    • sleep(long) – makes current thread sleep for given ms
    • yield() – makes current thread give up the CPU
    • currentThread() – returns the currently running thread object

Threads and Synchronisation in Java Calin Curescu TDDC32 lecture 1, 20064 of 9

Thread state

initial

running

ready

blocked

dead wait()

notify()

yield()

end of run()

scheduler scheduler

start()

  • wait(), notify() and join() used for synchronisation
    • we’ll come back in a couple of slides

join() sleep()

Threads and Synchronisation in Java Calin Curescu

5 of 9 TDDC32 lecture 1, 2006

Scheduling

  • How do two threads execute on
    • 2 processors?
    • 1 processor?
  • Round robin with time share
    • Usual systems
  • Priority based
    • Real-time systems

Threads and Synchronisation in Java Calin Curescu

6 of 9 TDDC32 lecture 1, 2006

Blocking and synchronisation

  • Blocking
    • Waiting for external or internal events
  • Critical section

Threads and Synchronisation in JavaCalin Curescu 7 of 9 TDDC32 lecture 1, 2006

Creating threads (2 ways)

  • Declare a subclass of class Thread (can be anonymous class) - Override run() in subclass - Create an instance of subclass - Call start()
  • Declare a class that implements interface Runnable
    • Implement run()
    • Create an instance of class
    • Create a new Thread instance and giving the Runnable instance as argument
  • Call start() in the Thread object
  • When run() returns the thread dies (and cannot be restarted)

Threads and Synchronisation in JavaCalin Curescu 8 of 9 TDDC32 lecture 1, 2006

Synchronisation

  • Threads run in parallel
  • What happens if they are not independent?
    • E.g. Producers-Consumers, readers-writers problems
    • They have to wait for each other depending on some conditions

Threads and Synchronisation in Java Calin Curescu TDDC32 lecture 1, 20069 of 9

Concept of Monitor

  • Delimits some critical code
  • Only one thread should be allowed inside the monitor at a certain time - Mutex lock (mutually exclusive)
  • Inside the monitor threads should be able to synchonise on certain conditions - “Condition variables” - When the thread is waiting for a condition to become true: it looses the lock on the monitor and another thread can enter - When the condition is fulfilled the thread has to acquire the lock again

Threads and Synchronisation in Java Calin Curescu TDDC32 lecture 1, 200610 of 9

Mutex in Java

  • Every object has a mutex lock
  • A critical section (block) can be specified with: synchronized(obj) { … critical … }
  • When a thread enters the synchronized block it automatically tries to take (lock) the lock of object obj
  • If successful, it continues, and automatically releases (unlocks) the lock when it exits the block
  • If not successful, it blocks until the thread that has the lock, releases it (i.e. it exits a block synchronised on that lock)
  • To synchronise an entire method on the lock of its object (this) we can also use: public synchronized method() { … critical … }

Threads and Synchronisation in Java Calin Curescu

11 of 9 TDDC32 lecture 1, 2006

Wait() and notify()

  • Methods of Object, not Thread
  • Can only be used in synchronised code
  • wait()
    • thread must wait on a synchronisation condition, releases the lock to let somebody else come in the critical code (to change the state for the better)
    • Is put on hold
  • notify()
    • Condition has changed
    • One of the threads waiting is woken up
    • Thread still has to fight for the lock
  • notifyall()
    • All the waiting threads are notified

Threads and Synchronisation in Java Calin Curescu

12 of 9 TDDC32 lecture 1, 2006

Additional Reading

  • The Java Tutorial
    • Trail: Essential Java Classes
    • Lesson: Threads: Doing Two or More Tasks at Once
    • But all lessons in the trail are fundamental
    • http://java.sun.com/docs/books/tutorial/essential/index.html