Home > Archive > Java Help > February 2005 > thread/interrupt question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
thread/interrupt question
|
|
| baobao 2005-02-23, 4:02 pm |
| 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!
| |
| Daniel Tahin 2005-02-23, 4:02 pm |
| 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:
> 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!
>
>
|
|
|
|
|