A program that has two or parts can run concurrently. Each part is a thread. So this is a specialised form multitasking.
Process based multitasking is heavyweight and use seperate address spaces. It gives the big picture of the application. JVM has control as it handled by os level. Thread based is light weight and use same address space. So inter communication between threads is easy. This gives detail view of the part it's running. JVM has full control.
Multithreading reduces idle time by running another when one is waiting for something like slow network operation.
And this avoids application being blocked for something.
In a single core CPU no actual parallel threading is happening but CPU time is allocated between threads.
Running thread can be suspended and resumed. Can be blocked when waiting. When terminated can't resume.
Threads has priorities. Higher priority threads can take cpu power by pushing low or low priority threads can volunteeraly allow if its blocked. For same priority threads windows will slice cpu time but on other os will till other finishers.
Once a thread is in a synchronized method, no other threads can access it. Java thread to thread communication can be done via synchronized methods.
Java multithreading is based on thread class and it's runnable interface. Dealing with thread is done using its proxy. Creation is done by extending thread class or implementing runnable interface.
Thread join will wait till a thread to terminate. Run is the entry point. Sleep suspend a thread for some time. Start calls run method.
You can obtain a reference to a thread by calling static Thread.currentThread () method.
Creating Threads
A thread can be created by implementing runnable interface or by extending thread class.
To implement runnable only method requires to implement in Runnable is run() it has all code to execute in new thread. Need to create a thread object within this class and call thread.start() which makes a calll to run() method.
Extending thread class. Must override run method and call start to begin new thread.
Runnable implementation allows multiple inheritance while extending helps more control.
IsAlive helps knowing status of a another thread. Join() can use to a wait till another thread finishes.
Thread priority decides CPU time for the thread. When priority is equal thread behaviour could be different based on the environment.
Synchronization
All threads trying to access same resource at same time is called race condition, all threads racing each other to complete the method. Synchronization Allows only one thread at a time to access a shared resource.
It has a lock called monitor to lock the resources. All objects have access to there monitors.
When one thread is entered into synchronized method other synchronized method in the same instance is also locked for the same thread.
Has two ways to use synchronize.
1. Synchronized void method () {}
Updating the methods
2. Synchronized (object) {}
Calling method within synchronized. Can use to call 3rd party classes.
Inter thread communication
is done using wait and notify method. Wait () will put the calling thread to sleep and leave the monitor until some other thread use the same monitor and call notify () or notify all()
Notify() wakes up the thread that called wait on the same object.
Wait should be called within a loop to check wait conditions as some times thread can start for no reason.
Deadlock
is possible when two thread try to access single resources concurrently
Thread control
Suspend resume and stop Is not allowed from Java version 2. This courses system errors. So it should be done using flags with wait and notify methods
Thread state
GetState() returns the state of the thread.
Blocked- waiting to acquire a lock
New - not begun execution
Runnable- either running or can run when CPU is provided
Terminated- execution completed
Timed waiting- when thread is in sleep and time out versions of sleep and join states
Waiting- non timeout versions of wait and join
Comments
Post a Comment