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

Java program for event Handelling

Java program for event Handelling

import java.awt.*;
import java.awt.event.*;
class EventTest implements ActionListener
{Frame fr;
Button b1,b2,b3,b4;
TextField tf1,tf2,tf3;
Label lb1,lb2,lb3;

EventTest( )
{ fr = new Frame("myframe" );
fr.setLayout(null);
b1= new Button ("Add") ;
b2= new Button ("Sub") ;
b3= new Button ("Mul") ;
b4= new Button ("Div") ;
tf1 = new TextField() ;
tf2 = new TextField() ;
tf3= new TextField() ;
lb1 = new Label("no1") ;
lb2 = new Label("no2") ;
lb3 = new Label("res") ;
tf1.setBounds( 50,50,100,50) ;
tf2.setBounds( 50,120,100,50) ;
tf3.setBounds( 50,190,100,50) ;
lb1.setBounds(10,50,35,50);
lb2.setBounds(10,120,35,50);
lb3.setBounds(10,190,35,50);
b1.setBounds( 30,260,35,50);
b2.setBounds( 85,260,35,50);
b3.setBounds( 140,260,35,50);
b4.setBounds( 195,260,35,50);
fr.add( tf1) ;
fr.add( tf2) ;
fr.add( tf3) ;
fr.add( lb1) ;
fr.add( lb2) ;
fr.add( lb3) ;
fr.add( b1) ;
fr.add( b2) ;
fr.add( b3) ;
fr.add( b4) ;
b1.addActionListener( this );
b2.addActionListener( this );
b3.addActionListener( this );
b4.addActionListener( this );

fr.setSize(600,700);
fr.setVisible(true);
}
public void actionPerformed (ActionEvent e )
{
if ( e.getSource() == b1 )
{
String str1 = tf1.getText() ;
String str2 = tf2.getText() ;
int s1 = Integer.parseInt(str1) ;
int s2 = Integer.parseInt(str2) ;
int result = (s1+s2) ;
String str3 =" " + result ;
tf3.setText ( str3) ;
}

if ( e.getSource() == b2 )
{
String str1 = tf1.getText() ;
String str2 = tf2.getText() ;
int s1 = Integer.parseInt(str1) ;
int s2 = Integer.parseInt(str2) ;
int result = s1-s2 ;
String str3 =" " + result ;
tf3.setText ( str3) ;
}
if ( e.getSource() == b3 )
{
String str1 = tf1.getText() ;
String str2 = tf2.getText() ;
int s1 = Integer.parseInt(str1) ;
int s2 = Integer.parseInt(str2) ;
int result = s1*s2 ;
String str3 =" " + result ;
tf3.setText ( str3) ;
}

if ( e.getSource() == b4 )
{
String str1 = tf1.getText() ;
String str2 = tf2.getText() ;
int s1 = Integer.parseInt(str1) ;
int s2 = Integer.parseInt(str2) ;
int result = (s1/s2) ;
String str3 =" " + result ;
tf3.setText ( str3) ;
}
}

public static void main (String s[] )
{
new EventTest();
}

}

Read More
admin March 17, 2014 0 Comments