Uploaded by Elena Boer

Threads

advertisement
1
2 Threads
• A Thread is a section of code executed independently from
other threads within a single program.
• Threads are lightweight processes (LWP): All threads within
the same process share the same address space.
• Threads can execute in parallel on a multi-core or
multi-processor system.
R. Tanner
Network Programming
2
2.1 Java Thread: Interface or Inheritance
There are 2 ways to define and start a Thread in Java:
• by implementing the Runnable interface,
• by subclassing (extending) the Thread class.
The main thread is started in the main method. All other threads
begin their execution in the run method.
Threads are terminated when they leave their run method.
R. Tanner
Network Programming
3
p u b l i c c l a s s ThreadEx implements Runnable {
@Override
p u b l i c v o i d run ( ) {
...
}
p u b l i c s t a t i c v o i d main ( String args [ ] ) {
( new Thread ( new ThreadEx ( ) ) ) . start ( ) ;
}
}
R. Tanner
Network Programming
4
2.2 Python Thread: Instance of Thread Class
Thread creation by passing a callable object to the constructor:
def thread_function ( p ) :
..
x = threading . Thread ( target=thread_function , args = ( 1 , ) )
x . start ( )
Alternative: overriding the run method in a subclass.
R. Tanner
Network Programming
5
2.3 Thread Interference: Race Condition
Interference happens when two operations, running in different
threads, but acting on the same data, interleave.
This means that the two operations consist of multiple steps, and
the sequences of steps overlap.
p u b l i c c l a s s Account {
p r i v a t e double balance = 0 ;
p u b l i c v o i d deposit ( double amount ) {
balance += amount ;
}
R. Tanner
Network Programming
6
p u b l i c boolean withdraw ( double amount ) {
i f ( balance − amount >= 0 ) {
// do some c o m p l i c a t e d p r o c e s s i n g
...
balance −= amount ;
return true ;
}
return f a l s e ;
}
}
R. Tanner
Network Programming
7
2.4 Synchronization
Race conditions can be prevented by using a mutual exclusion to
protect critical sections against concurrent access. The first thread
that enters a critical section grabs the lock. Any subsequent thread
will have to wait until the lock is released.
Java: synchronized methods and statements,
Python: threading.Lock object
lock = threading . Lock ( )
with lock :
...
R. Tanner
# c r i t i c a l section
Network Programming
8
2.5 Deadlock
A deadlock occurs when multiple threads are waiting for an action
that only one of the others can perform:
• Thread 1 holds a lock on Object A and is waiting for the lock
on Object B,
• Thread 2 holds the lock on Object B and is waiting for the
lock on Object A.
Neither thread will ever acquire the second lock or release the first
lock. They will simply wait forever.
R. Tanner
Network Programming
9
2.6 Conclusion
Adantages of using multiple threads:
Responsiveness: Making the user interface more responsive
when performing long tasks,
Performance: Exploiting multiprocessor systems to handle
multiple tasks in parallel,
Design: Simplifying modeling of simulations or agent-based
systems, performing asynchronous or background processing
R. Tanner
Network Programming
10
Disadvantage:
Unpredictable Behaviour: concurrent data access can produce
race conditions and deadlocks which are diļ¬€icult to debug.
R. Tanner
Network Programming
11
2.7 Exercises
1. Make the Java/Python class Account thread-safe.
2. Implement a console based multithreaded counter that
increments its counter value by 1 starting at 0 in 1 second
intervals and prints the resulting value in the console window.
When the user hits the ENTER key the counter value should
by decremented by 1. Each subsequent ENTER-Key event
should reverse the direction.
R. Tanner
Network Programming
12
2.8 Further Information
• Java Tutorials: Concurrency
docs.oracle.com/javase/tutorial/essential/concurrency
• Introduction to Java Threads
developer.ibm.com/technologies/java/tutorials/j-threads/
• Python threading – Thread-based parallelism
https://docs.python.org/3.8/library/threading.html
• An Intro to Threading in Python
realpython.com/intro-to-python-threading/
R. Tanner
Network Programming
Download