welcum to my blog..!!!!

get ur lab program codes with concept from here..(mainly for SASTRA'ites but can be useful to others as well.....

it contains many of my algorithms and programs for different problems and the lab exercises as well....

share ur suggestions and demands..!!

hope u'll like it...!!!
go ahead..!!!

Monday, July 26, 2010

ex3: lamport's algorithm..

here is my code to implement the program in java to order events in distributing environments using threads running in parallel.....(lamport' algorithm)

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class common extends Panel implements Runnable
{
public Label l1,l2,l3,l4,l5;
public TextField t1,t2,t3;
public Button b1;
public int tick;
public java.awt.List list1;
public Thread th;
Boolean flag;
public common(String str)
{
flag=true;
th=new Thread(this);
l1=new Label("time");
l2=new Label("message");
l3=new Label("to:");
l4=new Label("received");
l5=new Label(str);
t1=new TextField("0");
t2=new TextField();
t3=new TextField();
b1=new Button("send");
list1=new java.awt.List();
this.setVisible(true);
this.setLayout(null);
l5.setBounds(100,15,100,30);
l1.setBounds(50,70,100,20);
l2.setBounds(50,120,100,20);
l3.setBounds(50,180,100,20);
l4.setBounds(50,240,100,20);
t1.setBounds(150,70,100,20);
t2.setBounds(150,120,100,20);
t3.setBounds(150,240,100,20);
list1.setBounds(150,180,100,40);
b1.setBounds(150,350,100,20);
t1.setEditable(false);
this.add(l5);
this.add(l1);
this.add(t1);
this.add(l2);
this.add(t2);
this.add(l3);
this.add(list1);
this.add(l4);
this.add(t3);
this.add(b1);
this.setSize(300,400);
this.setBackground(Color.GRAY);
}
public void run()
{
try{
while(flag)
{
t1.setText((Integer.parseInt(t1.getText())+this.tick)+"");
Thread.sleep(500);

}
}catch(Exception e){;}

}

}

public class lamport implements ActionListener
{
Frame f;
common c1,c2,c3;
lamport()
{
f=new Frame("Rohit's lamport");
c1=new common("Process: A");
c2=new common("Process: B");
c3=new common("Process: C");
f.setLayout(null);
c1.setBounds(30,100,300,400);
c2.setBounds(350,100,300,400);
c3.setBounds(670,100,300,400);
f.add(c1);
f.add(c2);
f.add(c3);
c1.b1.addActionListener(this);
c2.b1.addActionListener(this);
c3.b1.addActionListener(this);
c1.list1.add("Process B");
c1.list1.add("Process C");
c2.list1.add("Process A");
c2.list1.add("Process C");
c3.list1.add("Process A");
c3.list1.add("Process B");
c1.tick=6;c2.tick=8;c3.tick=10;
f.setVisible(true);
f.setSize(1000,600);
f.setBackground(Color.BLACK);
c1.th.start();
c2.th.start();
c3.th.start();
}

public static void main(String arg[])
{
lamport lp=new lamport();

}
public void actionPerformed(ActionEvent ae)
{
String temp1,temp2,temp3;
if(ae.getActionCommand().equals("send"))
{
if((""+ae.getSource()).substring(16,23).equals("button0"))
{
temp1=c1.t1.getText();
temp2=c1.t2.getText();
if(c1.list1.getSelectedItem().equals("Process B"))
{
temp3=c2.t1.getText();
c2.t3.setText(temp2+","+temp1);

}
else if(c1.list1.getSelectedItem().equals("Process C"))
{
temp3=c3.t2.getText();
c3.t3.setText(temp2+","+temp1);

}
}
if((""+ae.getSource()).substring(16,23).equals("button1"))
{
temp1=c2.t1.getText();
temp2=c2.t2.getText();
if(c2.list1.getSelectedItem().equals("Process A"))
{
temp3=c1.t1.getText();
if(Integer.parseInt(temp1)>Integer.parseInt(temp3))
c1.t1.setText(temp1);
c1.t3.setText(temp2+","+temp1);
}
else if(c2.list1.getSelectedItem().equals("Process C"))
{
temp3=c3.t1.getText();
c3.t3.setText(temp2+","+temp1);

}

}
if((""+ae.getSource()).substring(16,23).equals("button2"))
{
temp1=c3.t1.getText();
temp2=c3.t2.getText();
if(c3.list1.getSelectedItem().equals("Process B"))
{
temp3=c2.t1.getText();
if(Integer.parseInt(temp1)>Integer.parseInt(temp3))
c2.t1.setText(temp1);
c2.t3.setText(temp2+","+temp1);

}
else if(c3.list1.getSelectedItem().equals("Process A"))
{
temp3=c1.t1.getText();
if(Integer.parseInt(temp1)>Integer.parseInt(temp3))
c1.t1.setText(temp1);
c1.t3.setText(temp2+","+temp1);

}

}
}

}




}


EX:1 (3 threads communicating-stop watch)

this code generates 3 threads needed for the stop watch which is ex.1 in distributed lab....


import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class stop_watch implements ActionListener
{
Frame f;
Button Start,Stop,Resume,Quit;
Label Normalclock,Stop1,Timer;
TextField t1,t2,t3;
Panel p1,p2,p3;
Thread th1,th2,th3;
boolean flag1,flag2,flag3,res1,res2,res3;
boolean fg;
Date date=new Date();



public class thread1 implements Runnable
{

public thread1()
{
th1=new Thread(this);
th1.start();
}
public void run()
{
while(true)
{

try{
int temp=Integer.parseInt(t3.getText());
//int temp2=temp;
while(flag1)
{

if(temp>0)
{
temp--;
Thread.sleep(5);
t3.setText(""+temp);
}
else{
t3.setText("10");
flag1=false;
flag2=true;

}
}
}catch(Exception e){;}
}
}

}





public class thread2 implements Runnable
{
public thread2()
{
th2=new Thread(this);
th2.start();
}
public void run()
{
while(true)

{

try{
int temp=Integer.parseInt(t3.getText());
int temp2=temp;

while(flag2)
{
if(Integer.parseInt(t2.getText())==9)
{

flag2=false;
t2.setText("0");
flag3=true;
}
else{
int temp1=Integer.parseInt(t2.getText());
temp1++;
Thread.sleep(5);
t2.setText(""+temp1);
flag2=false;
flag1=true;
}
}
}catch(Exception e){;}


}
}

}





public class thread3 implements Runnable
{
public thread3()
{
th3=new Thread(this);
th3.start();
}
public void run()
{
while(true)

{

try{
int temp=Integer.parseInt(t3.getText());
int temp2=temp;
while(flag3)
{

t2.setText("0");
if(Integer.parseInt(""+t1.getText().substring(3,5))<9)
t1.setText(t1.getText().substring(0,2)+":0"+(Integer.parseInt(""+t1.getText().charAt(4))+1));
else
t1.setText(t1.getText().substring(0,2)+":"+(Integer.parseInt(t1.getText().substring(3,5))+1));

flag3=false;
flag1=true;
}

}catch(Exception e){;}


}
}

}


public stop_watch()
{

f=new Frame("stop_watch");
Start=new Button("Start");
Stop=new Button("Stop");
Resume=new Button("Resume");
Quit=new Button("Quit");
Normalclock=new Label("clock");
Stop1=new Label("Stop");
Timer=new Label("Timer");
p1=new Panel();
p2=new Panel();
p3=new Panel();
t1=new TextField(5);
t1.setText((""+date).substring(11,16));


t2=new TextField(5);
t2.setText("0");
t3=new TextField(5);
t3.setText("10");
t1.setEditable(false);
t2.setEditable(false);
t3.setEditable(false);
Start.addActionListener(this);
Stop.addActionListener(this);
Resume.addActionListener(this);
Quit.addActionListener(this);
//p1.setLayout(new GridLayout(3,1));
p1.add(Normalclock);
p1.add(Stop1);
p1.add(Timer);
//p2.setLayout(new GridLayout(3,1));
p2.add(t1);
p2.add(t2);
p2.add(t3);
//p3.setLayout(new GridLayout(4,1));
p3.add(Start);
p3.add(Stop);
p3.add(Resume);
p3.add(Quit);
f.setLayout(new BorderLayout());
f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.CENTER);
f.add(p3,BorderLayout.SOUTH);

f.setVisible(true);
f.setSize(500,500);

}


public void actionPerformed(ActionEvent ae)
{
try{
if(ae.getActionCommand().equals("Start"))
{

thread1 w1=new thread1();
thread2 w2=new thread2();
thread3 w3=new thread3();
flag1=true;



}
if(ae.getActionCommand().equals("Stop"))
{
res1=flag1;
res2=flag2;
res3=flag3;
flag1=false;flag2=false;flag3=false;

//th1.suspend();
//th2.suspend();
//th3.suspend();


}
if(ae.getActionCommand().equals("Resume"))
{
flag1=res1;flag2=res2;flag3=res3;
notifyAll();

}
if(ae.getActionCommand().equals("Quit"))
{
System.exit(0);
}
}catch(Exception e){;}
}
public static void main(String arg[])
{
stop_watch sw=new stop_watch();

}

}