access violation using threads in Applet


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: access violation using threads in Applet

Hybrid View

  1. #1
    Join Date
    May 2005
    Posts
    2

    access violation using threads in Applet

    --------------------------------------------------------------------------------

    I am new to using threads and I am having a problem using them in applets Is there anyone who would help me solve this problem? I am running on windows XP.

    Here is the error message:

    java.security.AccessControlException: access denied (java.lang.RuntimePermission modifyThreadGroup)
    at java.security.AccessControlContext.checkPermission (AccessControlContext.java:270)
    at java.security.AccessController.checkPermission(Acc essController.java:401)
    at java.lang.SecurityManager.checkPermission(Security Manager.java:542)
    at sun.applet.AppletSecurity.checkAccess(AppletSecuri ty.java:169)
    at java.lang.ThreadGroup.checkAccess(ThreadGroup.java :287)
    at java.lang.Thread.init(Thread.java:286)
    at java.lang.Thread.<init>(Thread.java:332)
    at Ballbewegung1.start(Ballbewegung1.java:19)
    at sun.applet.AppletPanel.run(AppletPanel.java:371)
    at java.lang.Thread.run(Thread.java:536)



    Here is a copy of the code:

    import java.applet.*;
    import java.awt.*;

    public class Ballbewegung1 extends Applet implements Runnable
    {
    // Initialisierung der Variablen
    int x_pos = 10; // x - Position des Balles
    int y_pos = 100; // y - Position des Balles
    int radius = 20; // Radius des Balles

    public void init()
    {
    setBackground (Color.blue);
    }

    public void start ()
    {
    // Schaffen eines neuen Threads, in dem das Spiel läuft
    Thread th = new Thread (this);
    // Starten des Threads
    th.start ();
    }

    public void stop()
    {

    }

    public void destroy()
    {

    }

    public void run ()
    {
    // Erniedrigen der ThreadPriority um zeichnen zu erleichtern
    Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);

    // Solange true ist läuft der Thread weiter
    while (true)
    {
    // Verändern der x- Koordinate
    x_pos ++;

    // Neuzeichnen des Applets
    repaint();

    try
    {
    // Stoppen des Threads für in Klammern angegebene Millisekunden
    Thread.sleep (20);
    }
    catch (InterruptedException ex)
    {
    // do nothing
    }

    // Zurücksetzen der ThreadPriority auf Maximalwert
    Thread.currentThread().setPriority(Thread.MAX_PRIO RITY);
    }
    }


    public void paint (Graphics g)
    {
    g.setColor (Color.red);

    g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
    }

    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Some remarks:

    I cannot see anything wrong here (well some stuff...) so I'm thinking browser settings,
    as it is the Security manager that is giving you trouble.

    But, I cannot see the reason for this:

    Code:
    public void run() {
    // Erniedrigen der ThreadPriority um zeichnen zu erleichtern
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        System.out.println("Thread running"); // <-- I made this
    // Solange true ist läuft der Thread weiter
        while (true) {
    // Verändern der x- Koordinate
          x_pos++;
    
    // Neuzeichnen des Applets
          repaint();
    
          try {
    // Stoppen des Threads für in Klammern angegebene Millisekunden
            Thread.sleep(20);
          }
          catch (InterruptedException ex) {
    // do nothing
          }
    
    // Zurücksetzen der ThreadPriority auf Maximalwert
          Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        }
      }
    Run this code and check how many times the text :"Thread running" is
    written to the console. If this is for "ease of drawing" (if I got your german
    comments right) you should do it with double buffering. As it is now your
    applet sets the thread priority to MAX on every tick...
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center