Computer system normally has many active processes and threads;
true for single execution core as well.
Processing time for a single core is shared among processes and
threads through an OS feature called time slicing.
Thread lightweight processes
Thread has 2 strategies -
Directly control thread creation, management each time by the
Abstract thread management by passing the application's tasks
2 ways - implements Runnable interface and extends Thread class
Mostly implements Runnable interface used because can do
subclass, more flexible, used in high-level thread management.
Thread.sleep period can be terminated by interrupts
Thread.interrupted() - returns true if an interrupt has been
sleep, join methods are dependent on the OS for timing, so you
should not assume that join will wait exactly as long as you
InterruptedException - By sleep, join methods.
Synchronization prevent thread interference(Thread A's result is
lost, overwritten by Thread B) and memory consistency errors
Happens-before relationship is simply a guarantee that memory
writes by one specific statement are visible to another specific
final fields which can not be modified after the object is
constructed, can be safely read through non-synchronized methods,
once the object is constructed.
Synchronization is built around an internal entity known as the intrinsic
lock or monitor lock. Every object has an
intrinsic lock associated with it.
Reentrant synchronization - allowing a thread to acquire the same
lock more than once enables.
Atomic action - can not stop in the middle, either happens
completely or doesn't. Also no side effects of atomic action are
visible until the action is complete.
volatile keyword for all variables(long and double) do atomic
action(Reads and writes). Changes to a volatile variable are always
visible to other threads(due to happens-before relationship)
Starvation - situation where a thread is unable to gain regular
access to shared resources due to this unable to make progress.
Livelock - e.g. Thread often acts in response to the action of
another thread. If other thread's action is also a response to the
action of another thread or goes one....
How to create Immutable Objects? (likely thread env)
If object state cannot change after it is constructed then
called as Immutable Objects.
Don't provide "setter" methods
Make all fields final and private.
Declare the class as final.
Make the constructor private and construct instances in factory
Don't provide methods that modify the mutable objects
Don't share references to the mutable objects, never store
references to external, mutable objects passed to the constructor.
If require create copies and store references to the copies. Also
create copies of your internal mutable objects when necessary to
avoid returning the originals in your methods.
ThreadMXBean management interface for the thread system of the
Java virtual machine. Based on that you can get the Thread CPU time,
thread Contention Monitoring, Synchronization Information and
Deadlock Detection etc
Advantage of Multithreading
Increase performance of a program/Task
Advantage of Multithreading
Sometime unexpected results
Increased complexity of the code
Adding the overhead of thread creation.
Best Practices of Multithreading
Avoid using static objects -
Use local variables - Each thread that accesses the method would
have their own copy of objects - this would reduce the concurrency
Use locking judiciously - preferable to take advantage of the
lock over synchronized keyword