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();
}
}
No comments:
Post a Comment