| What is multithreading: |
Java support multithreading programing. A (multithreaded) program contains two or more parts that can run concurrently. Each part of such program is called a "Thread" and each thread defines seperate path of exectution. Every thread has own independent “call stack”. |
| What is Main Thread: |
| The main thread is created automatically when your program is started, it can controlled through a Thread object. You can obtain the reference of it by calling the method currentThread() which is a public static member of Thread. Case #1 package threads; Case #2 package threads; |
| Creating Threads: |
The thread is an independent sequential path of execution within a program. At runtime, threads in a program shares common memory space so they share both data and code (i.e. they are lightweight compared to process). The Thread class implement the Runnable interface and the Runnable interface has only one method that is public void run(). At minimum you have to know following methods of Thread class: Constructors of the Thread class: Implementing the Runnable interface:
When creating threads, implementing the Runnable interface is good programming practice because you can extend other class as well as implementing the Runnable interface. |
| Synchronization: |
| When two or more threads need access to shared resource, they need some way to ensure tha the resource will be used by only one thread a time. The process by which this is achieved is called Synchronization. Each resource in java has its own lock. So threads first obtain the lock then use that resource. You can synchronize your code in either of two ways - Synchronized Methods: To enter the object’s lock, you, just call a method that has been modified with synchronized keyword. While a thread is inside a synchronized method, all other threads that try to call it (or any other synchronized method) on same object have to wait. A thread relinquishes the lock simply by returning from synchronized method, allowing the next thread waiting for this lock to proceed. Synchronized blocks: Imagine that you want to synchronize access to objects of the class that was not designed for multithreaded access. That is this class does not use synchronized methods. This class was not created by you but by the third party and you have not right to change the code of that class. Solution of this problem is very simple: You simply put calls to the methods defined by this class inside a synchronized block. Synchronized(objectRef){ ObjectRef.method1(); // statements } |
| Interthread Communication: |
In Java, threads communicate using three methods – wait(), notify(), and notifyAll(). These methods are implemented in Object class, so all classes have these methods. All three methods can be called only from within a synchronized context.
|
| What is multithreading: |
| Java support multithreading programing. A (multithreaded) program contains two or more parts that can run concurrently. Each part of such program is called a "Thread" and each thread defines seperate path of exectution |
No comments:
Post a Comment