beingtopper.jkp@gmail.com
+91-8700702530
Free Demo

Being Topper

  • Home
  • About Us
  • Knowledge Base
  • Modules
    • Website Planning and Creation
    • Social Media Marketing Course
    • Search Engine Optimization
    • SEM/PPC Training
  • Branches
    • Ajmer
    • Tilak Nagar
    • Noida
    • Chandigarh
    • Mumbai
  • Contact Us

Being Topper

  • Home
  • About Us
  • Knowledge Base
  • Modules
    • Website Planning and Creation
    • Social Media Marketing Course
    • Search Engine Optimization
    • SEM/PPC Training
  • Branches
    • Ajmer
    • Tilak Nagar
    • Noida
    • Chandigarh
    • Mumbai
  • Contact Us
  • Home
  • About Us
  • Knowledge Base
  • Modules
    • Website Planning and Creation
    • Social Media Marketing Course
    • Search Engine Optimization
    • SEM/PPC Training
  • Branches
    • Ajmer
    • Tilak Nagar
    • Noida
    • Chandigarh
    • Mumbai
  • Contact Us

Being Topper

  • Home
  • About Us
  • Knowledge Base
  • Modules
    • Website Planning and Creation
    • Social Media Marketing Course
    • Search Engine Optimization
    • SEM/PPC Training
  • Branches
    • Ajmer
    • Tilak Nagar
    • Noida
    • Chandigarh
    • Mumbai
  • Contact Us
Algorithm Design and analysis ProgramsB.tech notesBCA NOTESDeadlockInformation technologyJava ProgramsPrograms/Algorithm

Java program on Deadlock

Being Topper 17/03/2014 0 Comments

Before Checking Deadlock Program in java Lets Understand  the meaning of Deadlock in Java

What is Deadlock in Java?

 

Deadlock in java is a part of multi threading. Deadlock can occur in a situation when a thread is waiting for an object lock, that is acquired by another 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.

 

Java Keywords that are synchronized are to make class thread-safe which means only one of the method has the lock of the synchronized method which can use it.

If the program that we are operating is running in a multi-threaded environment where two or more than two of the threads are working together. But this is not mandatory to work all the time this type of problem is called Deadlock in Java.

 

Java program on Deadlock

Here is Example of Java Program on Deadlock

class Resource
{ int sum;

synchronized void sumCalc ()
{
String tname = Thread.currentThread().getName() ;
System.out.println(tname + " is starting to calculate the sum ....." );
for(int i = 1 ; i<= 10 ; i++)
{
int x = (int ) (Math.random() *100 );
sum+=x ;
}
System.out.println("sum of " + tname + " : " +sum);
if(tname.equals ("first"))
{
System.out.println(tname + "is getting sum of the other thread .....");
int otherSum = Factory.r2.getSum() ;
int total = sum + otherSum ;
System.out.println("total sum in "+ tname + " = "+total );
}
if(tname . equals ("second"))
{
System.out.println(tname + "is getting sum of the other thread .....");
int otherSum = Factory.r1.getSum() ;
int total = sum + otherSum ;
System.out.println("total sum in "+ tname + " = "+total );
}
}

synchronized int getSum()
{
return (sum );
}

}

class Factory
{
static Resource r1,r2 ;
static {r1 = new Resource();
r2 = new Resource() ;
}
}
class MyThread extends Thread
{
MyThread (String name )
{
super (name );
start() ;

}
public void run ()
{
String tname = getName () ;
if(tname.equals ("first"))
{
Factory.r1.sumCalc() ;

}
if(tname.equals ("second"))
{
Factory.r2.sumCalc() ;

}

}
}

class DeadlockTest
{
public static void main(String s[] )
{
new MyThread ("first");
new MyThread ("second");
}
}
java Java program on Deadlock program for deadlock
777 Views
AboutBeing Topper
In Socials:
Prevjava program for counting vowels , consonants , digits and whitespaces ...17/03/2014
java program to make a GUI based calculator using frames01/04/2014Next

Related Posts

BCA NOTESVB.Net progrmas

VBScript Full Program for structure | Structure in VBScript

Before Writing a program for structure in VBScript we have to know what is a structure...

Being Topper 18/01/2014
AppletB.tech notesBCA NOTESInformation technologyJava ProgramsPrograms/Algorithm

Java Program for working Pacman game

Java Program/source code for working Pacman Game. This program is primarily based on...

Being Topper 18/02/2014
Categories
Recent Posts
  • Vipin Khutail Organizes International Conference for Digital Excellence 2, Elevating the Future of Technology
  • India’s Top Occult Science Enthusiasts Gather at AgyatOnSearch – Awards & Seminar in New Delhi
  • India’s Leading Digital Marketer, Vipin Khutail Interacted with Top Influencers of Delhi at BJP West Delhi’s Event
  • Vipin Khutail Organized Awards & Honor Ceremony to Celebrate Social Reformers on Doctor’s Day
  • Vipin Khutail organized International Conference on Educational Excellence & Honored Visionaries

Copyright © 2021 Being Topper ® . All Rights Reserved