Results 1 to 5 of 5
  1. #1
    zf1
    zf1 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default way from executing thread to JApplet

    Hi folks,
    when an applet running in two instance (eg. in two tab in firefox) in the same browser then static variables are shared between the two instance, so statics cannot be used for implementing global variables.
    It seems to be a good idea to maintain only one public static Map-like object that holds one set of globals for every applet instance as values, and the key may be an object that somewhat refers to the applet instance - this object need to be derived from the thread that actually runs the applet's methods. Unfortunately i found that the same threads and threadgroups are operating on all the applet instances in one browser (jvm).
    Is there any way to get any information from a thread that identifies the applet instance (JApplet object?) itself where the thread is operating actually?

  2. #2
    JavaRulez is offline Member
    Join Date
    May 2010
    Posts
    26
    Rep Power
    0

    Default

    when an applet running in two instance (eg. in two tab in firefox) in the same browser then static variables are shared between the two instance
    This may be true, but it isn't guaranteed.

    this object need to be derived from the thread that actually runs the applet's methods. Unfortunately i found that the same threads and threadgroups are operating on all the applet instances in one browser (jvm).
    This would be the Event Dispatch Thread I believe, and from my understanding there should only be the one.

    It occurs to me that using a static mapping is a quite complicated manner of implementing instance variables. Where is this information needed? Is there a separate thread from which you are accessing these variables? Could you not acquire a reference to the JApplet, and then use getter and setter methods to manipulate these values?

  3. #3
    zf1
    zf1 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Where is this information needed?
    Everywhere. There are some class instances (globals) grouped in one 'holder' class. Since now the holder had public static members to provide access to these globals from all of the app's methods through HolderClass.globalMember form. But finally i realized that static members are shared (or may be shared) between applet instances, therefore i'm looking for a way to change statics to instances. But without rewriting hundreds of methods to pass holder instance in.

    Is there a separate thread from which you are accessing these variables?
    Yes there are more threads, but i think it does not matter in this case because if there was only one EDT i would still need to get the reference to the holder shomehow in the deeps of method calls.

    Could you not acquire a reference to the JApplet
    Yes, i'm looking for a way to do this exactly... :) without passing at least one reference through many-many method calls.

    In other words: there is a method running on a specific thread operating on a specific object instance - and this method needs to determine the JApplet instance where the object ('this' in the method) "lives in" without getting it as a parameter.

  4. #4
    JavaRulez is offline Member
    Join Date
    May 2010
    Posts
    26
    Rep Power
    0

    Default

    Well you've kind of stumped me as I'm unsure of an elegant way to solve your problem. From the sound of it I would suggest refactoring your code but you said:
    But without rewriting hundreds of methods to pass holder instance in.
    Depending on what is actually done by this chain of method calls, I'm thinking that you could create a new class which extends Thread, say MyThread, which contains the reference you desire. Then, the first method call of any given sequence could be called thusly:

    Java Code:
    new MyThread(JAppletReference, new Runnable() {
      public void run() {
        firstMethodCall();
      }
    }).start();
    -Edit- A call to join may be necessary if you wish to wait on the completion of these methods

    Then on down the chain, all your methods will be executing on this thread. Retrieve the reference simply:

    Java Code:
    ((MyThread) Thread.currentThread()).getJAppletReference();
    I'm not sure if it is the best way, but it is the first thing I thought of.
    Last edited by JavaRulez; 04-05-2011 at 03:05 PM.

  5. #5
    zf1
    zf1 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Hm, hm, thanks, it may be a good solution for threads created by my applet.
    But EDTs are created by jre so i can't "inject" a derived thread to the system.

Similar Threads

  1. JApplet on MAC
    By ld_pvl in forum Java Applets
    Replies: 0
    Last Post: 08-23-2009, 02:40 PM
  2. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 07:14 AM
  3. How to stop thread from being jumping off the code without executing it.....
    By chiragkini in forum Threads and Synchronization
    Replies: 6
    Last Post: 01-22-2009, 04:38 AM
  4. JApplet Problem
    By tanmoy.b81 in forum AWT / Swing
    Replies: 2
    Last Post: 07-28-2008, 07:56 AM
  5. help with converting to JApplet
    By Simmy in forum AWT / Swing
    Replies: 2
    Last Post: 08-09-2007, 09:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •