AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

starting a thread in applet

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
shyamvs.prasad@gmail.com
Guest





PostPosted: Thu Nov 23, 2006 11:36 pm    Post subject: starting a thread in applet Reply with quote



i have an applet and i want to do this :-
when the button in it is pressed :-
in the actionperformed of the button i want to set a flag to true;

another method must somehow know that the flag is set and that method
must start executing

so lemme tell u what i did :-
in the init() of the applet i gave
Thread t = new Thread(this); thread.start()

then in run() i gave
try{ System.out.println("in run"); t.sleep(1000);
if(flag){methodname();System.out.println("in run if");
t.sleep(1200);}
}
catch(Exception e){System.out.println("exception");}

but what happens in that "in run" is printed only once at the beginning
how do i accomplish what i want to ?
please need help !!!
Back to top
Knute Johnson
Guest





PostPosted: Fri Nov 24, 2006 12:37 am    Post subject: Re: starting a thread in applet Reply with quote



shyamvs.prasad (AT) gmail (DOT) com wrote:
Quote:
i have an applet and i want to do this :-
when the button in it is pressed :-
in the actionperformed of the button i want to set a flag to true;

another method must somehow know that the flag is set and that method
must start executing

so lemme tell u what i did :-
in the init() of the applet i gave
Thread t = new Thread(this); thread.start()

then in run() i gave
try{ System.out.println("in run"); t.sleep(1000);
if(flag){methodname();System.out.println("in run if");
t.sleep(1200);}
}
catch(Exception e){System.out.println("exception");}

but what happens in that "in run" is printed only once at the beginning
how do i accomplish what i want to ?
please need help !!!


public class XXX extends Applet implements ActionListener, Runnable {
public void init() {
Button b = new Button("Button");
b.addActionListener(this);

add(b);
}

public void actionPerformed(ActionEvent ae) {
String ac = ae.getActionCommand();
if (ac.equals("Button"))
new Thread(XXX.this).start();
}

public void run() {
while (???) { // while some condition is true, eg true
// do whatever
// if you want to sleep in the thread, use Thread.sleep()
// Thread.sleep() is a static method that sleeps the
// currently running thread
}
// when condition isn't true the thread will end
}



--

Knute Johnson
email s/nospam/knute/
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.