Thread basics

21
Thread basics

description

Thread basics. A computer process. Every time a program is executed a process is created It is managed via a data structure that keeps all things memory related Global address space File-handle table Heap data. A computer process. When swapping processes for execution: - PowerPoint PPT Presentation

Transcript of Thread basics

Page 1: Thread basics

Thread basics

Page 2: Thread basics

A computer process

• Every time a program is executed a process is created

• It is managed via a data structure that keeps all things memory related– Global address space– File-handle table– Heap data

Page 3: Thread basics

A computer process

• When swapping processes for execution:– All things in that d.s. have to be brought to disk– Large chunks of memory contents

• Process lots of memory• Swapping processes is expensive, lots of

memory have to be moved around.• Swapping (context swapping) is measured

in seconds

Page 4: Thread basics

Thread

• Sequence of byte code instructions executed by the JVM; thus a thread consists of two things:– A sequence of code– Its execution

• This sequence forms a path of code through a program sequence

• No notion of objects or methods here• Sequences of instructions (threads) can overlap,

also they can execute simultaneously on multiple threads

Page 5: Thread basics

Thread data structure

• It contains data to keep track of this sequence and its execution– Contents of the registers– Position of execution in the instruction

sequence– Run-time stack (LIFO) made up of activation

records (created for methods to contain local variables, parameters, return pointer)

– Life-time: threads are created and terminated.

Page 6: Thread basics

Swapping threads

• OS typically swaps a thread– Push registers on the thread’s stack– Add thread d.s. to an OS list– Pull a different thread’s d.s off that list– Pop thread’s local stack into register set.

• Thread swapping is measured in milliseconds. – Thus: threads are lightweight processes

• A process is equivalent to Java’s virtual machine• A thread is process state, ie a virtual machine state.

Page 7: Thread basics

Threads and Processes• Important: threads share the memory allocated to the process. • Thus, given two threads:

– May share instruction sequence– May share memory

• As a consequence:– Two threads may share program’s data and methods to modify it

• This is what makes threads hard to program. You need to carefully design for:– The commands executed by possibly multiple threads– The queries executed by possibly multiple threads– The time when that execution must occur.

• How to design threaded programming: – By synchronizing (locking) the actions caused by each thread that

shares data with other threads.

Page 8: Thread basics

Managing threads

• If a thread manipulates data that is not shared by any other thread, no need to manage this sequence of execution, no need for synchronization.

• Example of such case. Given two threads, each has its own stack:– Two threads do not share methods local variables and

parameters.

• If a method does not access any heap data ( thus no access to objects and their fields), it can be executed simultaneously by multiple threads without explicit synchronization.

Page 9: Thread basics

Multi-threaded programming

• Modern OS allow multiple threads to be running concurrently within a process.

• When JVM is started– A new process is created– Many threads can be created (spawned) in it.– One thread spawned is the thread having as

execution sequence given by the “main”.

Page 10: Thread basics

Default threads in JVM

• The “main” thread:– Spawned by the JVM when the process is

created. – Beginning sequence is given by the main().– Executes all statements in main()– Dies when main() completes execution.

Page 11: Thread basics

Default threads in JVM

• The “garbage collection” thread:– Spawned by the JVM when the process is

created. – The sequence of code it executes is the code

written to discard objects and reclaim memory.

• Thus any Java program has at least two threads executing: we say that a Java program runs in a multi-threaded environment.

Page 12: Thread basics

Default threads in JVM

• If the program contains a GUI interface a new thread is created: The “event-handling” thread: The sequence of instructions it executes is – the code associated with listeners.– Events coming from the OS.

• This means that if this thread is executing code of a listener, any mouse click or key press is put on hold until this thread terminates what is doing.

• As a consequence we have an unresponsive user interface: one that appears to hang.

Page 13: Thread basics

Thread safety• The phrase “thread safe” is used to describe a

method that can run safely in a multi-threaded environment– Accesses process-level data (shared memory by several

processes) in a safe and efficient way.

• The goal in designing multi-threaded programs is to achieve thread safety among other properties.

• This is usually a hard goal to achieve, one you must design up-front about, and one whose bugs are not only hard to pin-point but difficult to remove.

Page 14: Thread basics

Why multi-threaded programming?

• Better interaction with user• Simulation of simultaneous activities in one

processor machine• Exploitation of multi-processor machines• Execution of computation code while doing I/o.

Specially useful in network programming.• Allow multiple user to execution same code

simultaneously . Specially useful in network programming.

• Better Object oriented design.

Page 15: Thread basics

Why understanding and using threads

• All nontrivial applications for the Java platform are multithreaded, whether you like it or not.

• It's not ok to have an unresponsive UI.

• It’s not ok for a server to reject requests.

Page 16: Thread basics

When multi-threaded programming might not be good

• Threads carry overhead both in space and time.

• Thread management.

• All this cost must be considered when considering designing for threads.

Page 17: Thread basics

Thread behavior is platformdependent!

• You need to use the OS threading system to get parallelism (vs. concurrency)– Different operating systems use different

threading models (more in a moment).– Behavior often based on timing.– Multi-threaded apps can be slower than

single-threaded apps (but be better organized)

Page 18: Thread basics

Priorities

• The Java™ programming language has 10 levels• The Solaris™ OS has 2 31 levels• NT™ offers 5 (sliding) levels within 5 "priority classes."• NT priorities change by magic.

– After certain (unspecified) I/O operations priority is boosted(by an indeterminate amount) for some (unspecified) time.

– Stick to Thread.MAX_PRIORITY,– Thread.NORM_PRIORITY,Thread.MIN_PRIORITY)

Page 19: Thread basics

Threading models

• Cooperative (Windows 3.1)– A Thread must voluntarily relinquish control of the CPU.– Fast context swap, but hard to program and can’t

leveragemultiple processors.• Preemptive (NT)

– Control is taken away from the thread at effectively random times.

– Slower context swap, but easier to program and multiple threads can run on multiple processors.

• Hybrid (Solaris™ OS, Posix, HPUX, Etc.)– Simultaneous cooperative and preemptive models are

supported.

Page 20: Thread basics

Do not assume a particular environment

• Assume both of these rules, all the time:– A thread can prevent other threads from

running if it doesn't occasionally yield• by calling yield(), performing a blocking I/O

operation, etc.

– A thread can be preempted at any time byanother thread

• even by one that appears to be lower priority than the current one.

Page 21: Thread basics

Conclusion

• Easy to start using threads, VERY tough to master.

• Designing classes that contain methods that are thread safe is a challenge.

• Read: javadoc for the class java.lang.Thread