Thursday, February 18, 2010

What is Multithreading?


If two or more threads can run through your program concurrently, then that is known as multi threading:
  • Advantages : Multithreading can make the execution faster by making use of the unused resources by one thread at some point of time.
  • Disadvantages : Concurrent execution of multiple threads can lead to delay in the completion of individual thread due to sharing of resources.
Improper use of multithreading can lead to corrupted data. To avoid this, we can use synchronization. Synchronization is used to ensure that only one thread executes in a critical region at a time. Critical Regions are places where modification in the data is done. Ideally, only one thread should be executing in the critical regions.
Synchronization can be achieved by two ways:
  • Synchronized blocks:  We use synchronized blocks when we intend only a few lines of code to be synchronized.
class myClass {
    myMethod() {
      synchronized (object) {
         statement block
      }
   }
}
In this case, the object specified in the statement is the lock.
  • Synchronized methods : We use synchronized methods when we want the complete method to be synchronized.
class myClass {
   synchronized type myMethod() {
      statement block
   }
}
In this case, lock is obtained on the object being referenced in the synchronized method. Two synchronized methods referring to the same object can not be called simultaneously from two different threads.

Deadlock :  When two or more threads are waiting for each other to release some resource, they are unable to proceed with their own executions and keep on waiting. This condition is known as deadlock.
Deadlock can also include more than two threads. This happens when more than two thread are waiting for another thread to release the lock over a resource. See an example below:
Thread 1 waits for Resource A locked by Thread 2
Thread 2 waits for Resource B locked by Thread 3
Thread 3 waits for Resource C locked by Thread 1

9 comments:

  1. Great article, I need more information. Is there a visual representation of actual hardware and CPU events as they happen in real time. I am at home online student of microprocessor operations.My goal is to cheat the entire system and gain a college level education for the least amount of money since I'm unemployed and vastly in love with Computer technology. I want to know everything it takes to understand machine language so i can know more than any rich kid who has unlimited funds available from his(her) parents or so called govt grants gtgust@comcast.net

    ReplyDelete
  2. Thank you for the wonderful article..
    I have a question:
    How does Java map the threads that users create in Java (using "new Thread()") to kernel threads?
    Is it 1-to-1 or many-to-one or many-to-many mapping?

    (Here I am referring to the user thread to kernel thread mapping that we talk about in Operating Systems.. Just wanted to know how Java does the mapping..)

    ReplyDelete
  3. Each kernel thread is assigned to a certain number of user threads

    ReplyDelete
  4. Why multithreading increases performance while, on a single processor, only one thread can execute at a time?

    ReplyDelete
  5. multithreading has nothing to do with multiple processors. Multithreading can occur on single processor. Mutithreading increases performance because several threads perform the action and resources are utilized as much as possible.

    ReplyDelete
  6. multi threading on a single processor degrades the performance because of time spending on context switching. Multi threading is useful on multi core machines.

    ReplyDelete