File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes notify,thread,applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "notify,thread,applet" Watch "notify,thread,applet" New topic
Author

notify,thread,applet

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jul 28, 2000
Posts: 1850
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

Joined: Nov 22, 2008
Posts: 18944
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();
}
}



}
}

 
Don't get me started about those stupid light bulbs.
 
subject: notify,thread,applet