Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

notify,thread,applet

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a written a applet that uses wait and notify,but it is not working,,
notify and wait will not work in applet?
if you are interested i shall put the code..plz answer
 
Rahul Mahindrakar
Ranch Hand
Posts: 1868
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There should be no problem in using wait,notify , notifyAll() in an applet. Could you check up your code once more and if you still have problems put up the code over here.
Regds.
Rahul
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you rahul for ur reply,it is not working, i am putting my code here...please tell me the problem
------------
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code=Count2 width=250 height=250>
</applet>*/

public class Count2 extends Applet implements ActionListener
{
int c=0;
String out;
Counter1 c1;
Restart r1;
public Label l1;
Thread t;
volatile boolean pauseflag=true;
volatile boolean waitflag=false;
Count22 c22=new Count22();
public void init()
{
Button b1=new Button("Start");
Button b2=new Button("Pause");
l1=new Label();
b1.addActionListener(this);
b2.addActionListener(this);
add(b1);
add(b2);
add(l1);
c1= new Counter1();
r1=new Restart();
}
public void actionPerformed(ActionEvent an)
{


if(an.getActionCommand().equals("Start"))
{
System.out.println("start");
out=null;
c=0;

}

if(an.getActionCommand().equals("Pause"))
{
pauseflag=!pauseflag;

}
}
//first thread ,increments the count and displays ..,and when pause button clicked it will go to sleep mode..
class Counter1 implements Runnable
{
Counter1()
{
Thread tty= new Thread(this,"newone");
tty.start();
}
public void run()
{
c22.cc();

}
}
//second thread,each time it will call the notify,so if the other thread in sleep mode it will bring to ready to run mode
class Restart implements Runnable
{
Restart()
{
Thread tty1= new Thread(this,"newone");
tty1.start();
}
public void run()
{
c22.cc1();

}
}


class Count22
{

synchronized void cc()
{


for(;
{
if(!pauseflag)
{
System.out.println("before wait");

try{
//if pasuse is clicked once , goes to sleep
wait();
}
catch(InterruptedException ie){}
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException e){}


c++;
out=Integer.toString(c);
l1.setText(out);

}

}

synchronized void cc1()
{
while(true)
{

notify();
}
}



}
}

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic