Category: Algorithm Design and analysis Programs

Java program on Deadlock

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");
}
}

Read More
admin March 17, 2014 0 Comments

C++ PROGRAM FOR ACTIVITY SELECTOR

  WRITE A PROGRAM FOR ACTIVITY SELECTOR

 

#include<iostream.h>
 #include<conio.h>
 void main()
 {int act[3][7],i,j,temp,start,a[7],k=1,min;
 clrscr();
 for(i=0;i<7;i++)
 a[i]=0;
 for(i=0;i<7;i++)
 act[0][i]=i+1;
 for(j=0;j<7;j++)
 {cout<<"\nEnter the starting time of "<<j+1<<" activity:";
 cin>>act[1][j];
 cout<<"Enter the finishing time of "<<j+1<<" activity:";
 cin>>act[2][j];
 }
 for(i=0;i<7;i++)
 {min=act[2][i];
 start=act[1][i];
 temp=i;
 for(j=i+1;j<7;j++)
 {if(act[2][j]<min)
 {min=act[2][j];
 start=act[1][j];
 temp=j;
 }
 }
 act[2][temp]=act[2][i];
 act[1][temp]=act[1][i];
 act[2][i]=min;
 act[1][i]=start;
 }

a[0]=1;
 i=0;
 for(int m=1;m<7;m++)
 {if(act[1][m]>=act[2][i])
 {a[k]=m+1;
 k++;
 i=m;
 }
 }
 cout<<"\The activities that can be performed is:";
 for(i=0;a[i]!=0;i++)
 cout<<a[i]<<" ";
 getch();
 }
C++ PROGRAM FOR ACTIVITY SELECTOR WITH OUTPUT ..


C++-PROGRAM-FOR-ACTIVITY-SELECTOR

 

Read More
admin May 25, 2013 0 Comments