à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday, 8 October 2017

Multithreading in Java 6


Multithreading in Java 6

Deadlock in java

Deadlock in java is a part of multithreading. Deadlock can occur in a situation when first thread is waiting for an object lock, that is acquired by second thread and second thread is waiting for an object lock that is acquired by first thread. Since, both threads are waiting for each other to release the lock, the condition is called deadlock.

Example of deadlock :

class Pen{}
class Paper{}

public class Write {

  public static void main(String[] args)
  {
     final Pen pn =new Pen();
     final Paper pr =new Paper();
     
        // t1 tries to lock pn then pr  
	Thread t1 = new Thread(){
            public void run()
            {
                synchronized(pn)
                {
                    System.out.println("Thread1 is holding Pen");
                  try{
                      Thread.sleep(1000);
                  }catch(InterruptedException e){}
                    synchronized(pr)
                {  System.out.println("Requesting for Paper"); }
                    
                }
            }
        };
	
	// t1 tries to lock pr then pn
        Thread t2 = new Thread(){
            public void run()
            {
                synchronized(pr)
                {
                    System.out.println("Thread2 is holding Paper");
                  try{
                      Thread.sleep(1000);
                  }catch(InterruptedException e){}
                    synchronized(pn)
                {  System.out.println("requesting for Pen"); }
                    
                }
            }
        };
        
        t1.start();
        t2.start();
  }
    
}

Output :

Thread1 is holding Pen
Thread2 is holding Paper

No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website