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 

thread/interrupt question

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





PostPosted: Wed Feb 23, 2005 8:37 am    Post subject: thread/interrupt question Reply with quote



Ok, I am writing an applet and I spawn a thread when a certain menu item is
selected in order to do some computation without tieing up the UI. It looks
something like this:

public void actionPerformed(ActionEvent evt) {
Object source=evt.getSource();
//....
runner = new Thread(this);
runner.start();
}

In the stop() method of the applet I want to stop this thread so I do
something like this:

public void stop() {
if (runner != null) {
runner.interrupt();
try {
runner.join();
} catch (InterruptedException e) {
}
runner = null;
}
}

However, this code gives me the following exception:

java.Security.AccessControlException: access denied
(java.lang.RuntimePermission modifyThread)

Now, I have another way to get this to work but what I am wondering is why
when I spawn the thread in the start() method of the applet as in

public void start() {
runner = new Thread(this);
runner.start();
}

and use the same stop() method as above I do not get an exception when I
terminate the applet. Can someone please explain to me what is going on
here? It appears that the UI thread does not have "permission" to interrupt
the runner thread but whatever thread it is that calls start() of the applet
(what is the name of this thread by the way) does have "permission" to
interrupt the runner thread. Could someone please clarify what is going on
here?

Thanks in advance!


Back to top
Daniel Tahin
Guest





PostPosted: Wed Feb 23, 2005 1:36 pm    Post subject: Re: thread/interrupt question Reply with quote



Hi!

I'm not sure, what is exactly the problem, but i would say, you should
realize this code on an other way. I'll short explain it.
So i guess you extend the class java.applet.Applet, and implement
java.lang.Runnable in your class (otherwise you can't do runner = new
Thread(this)).
Perhaps the problem is, that java.applet.Applet and java.lang.Thread has
the same function "public void start()". And if you call
runner.start(), i'm not sure, that the correct thing happens.
Try to separate your class and the thread, like this:

//Put YourClass and Runner in the same file!
public YourClass extends java.applet.Applet
{
private Thread r;

public void actionPerformed(ActionEvent evt) {
r = new Runner();
r.start();
}

public void start() {
//you can instantiate r, here as well, when needed
}

public void stop() {
if (r != null) {
r.interrupt();
try {
r.join();
} catch (InterruptedException e) {
}
r = null;
}
}
}

class Runner extends Thread
{
public Runner("argument, whatever you want") {
}

synchronized public void run() {
//do the computation
}
}




baobao wrote:
Quote:
Ok, I am writing an applet and I spawn a thread when a certain menu item is
selected in order to do some computation without tieing up the UI. It looks
something like this:

public void actionPerformed(ActionEvent evt) {
Object source=evt.getSource();
//....
runner = new Thread(this);
runner.start();
}

In the stop() method of the applet I want to stop this thread so I do
something like this:

public void stop() {
if (runner != null) {
runner.interrupt();
try {
runner.join();
} catch (InterruptedException e) {
}
runner = null;
}
}

However, this code gives me the following exception:

java.Security.AccessControlException: access denied
(java.lang.RuntimePermission modifyThread)

Now, I have another way to get this to work but what I am wondering is why
when I spawn the thread in the start() method of the applet as in

public void start() {
runner = new Thread(this);
runner.start();
}

and use the same stop() method as above I do not get an exception when I
terminate the applet. Can someone please explain to me what is going on
here? It appears that the UI thread does not have "permission" to interrupt
the runner thread but whatever thread it is that calls start() of the applet
(what is the name of this thread by the way) does have "permission" to
interrupt the runner thread. Could someone please clarify what is going on
here?

Thanks in advance!



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.