The Artima Developer Community
Sponsored Link

Java Answers Forum
Old Threads

4 replies on 1 page. Most recent reply: Mar 24, 2005 7:52 AM by John Neale

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
cBin

Posts: 13
Nickname: cbb
Registered: Mar, 2005

Old Threads Posted: Mar 23, 2005 6:11 AM
Reply to this message Reply
Advertisement
I am trying to learn how to use threads in applets so I am looking at some old examples (from 1999) from the java.sun pages. I have JDK 1.5, and they will compile ok, but when I try to run the applet it complains and gives me this error
java.security.AccessControlException: access denied (java.lang.RuntimePermission modifyThreadGroup)

Here is what I have
class ThisPanel extends Panel implements Runnable{
   Thread thread;
 
   public void run() {
      Thread me = Thread.currentThread();     
      while (thread == me) {
	    // CALL A METHOD                           
            
           try {                               
	        Thread.sleep(100);
           } catch (InterruptedException e) {
		break;
	   }
      }
   } // END OF RUN
 
   public void start() {
       thread = new Thread(this);
       thread.start();
   }
 
   public void stop() {    
       thread = null;
   }
}
 
and the call from the applet class
    ThisPanel panel;
 
    public void start() {
	panel.start();
    }
 

any thoughts on why this is happening, and how I can use a simple thread like this one in a JApplet?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Old Threads Posted: Mar 24, 2005 12:02 AM
Reply to this message Reply
I don't know if this solves your problem, but:
You used
Thread.sleep(100);

This causes the current Thread to sleep. I think to remember that this is not allowed (with a few exceptions).

Try
thread.sleep(100);
instead.

Hint: to prevent errors like this, you should not give the variable the same name as the class.

cBin

Posts: 13
Nickname: cbb
Registered: Mar, 2005

Re: Old Threads Posted: Mar 24, 2005 3:38 AM
Reply to this message Reply
Thanks for the advice, but that was actually just a typo posting it.

I tried a few other things, including the SwingWorker class that is on the swing webpages, but I always get that AccessControlException when I try to run the applet

They do work when I just write them as a normal application with a main method. Is there something else that I need to know about using threads in applets?

Any other thoughts anyone??

cBin

Posts: 13
Nickname: cbb
Registered: Mar, 2005

Re: Old Threads Posted: Mar 24, 2005 6:42 AM
Reply to this message Reply
I figured it out!! there is nothing wrong with my thread code, it is just the applet viewer and/or NetBeans that doesn't like it. The applet works fine if I open the HTML file in my web browser.

John Neale

Posts: 10
Nickname: rhino
Registered: Oct, 2003

Re: Old Threads Posted: Mar 24, 2005 7:52 AM
Reply to this message Reply
You are getting a security exception. This is due to the JRE's security settings. By default applet security is quite strict and only allows you to play in the "sandbox". I think you need to sign the applet and include different security settings to modify the ThreadGroup.

I don't know the details of how to do this but check out http://java.sun.com/docs/books/tutorial/applet/practical/security.html whick should help.

Flat View: This topic has 4 replies on 1 page
Topic: Updating JLabel shows the preious text behind. Previous Topic   Next Topic Topic: why not public?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use