Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
New Developer
Posts: 22
Registered: ‎04-07-2008
My Device: Not Specified
Accepted Solution

Static Data

Hi,

 

I have a strange problem. I am not an expert in BB development. May be the solution is simple enough but not sure which one.

 

I have a class which has to public static functions which sets/gets a static flag value.

 

Access  to the class is being done from different packages, but everytime I query the flag value it always returns FALSE. From the defination of static variables, its copy is created only once and all the objects of the class share the same value. 

 

Following is the code snipped

    public static boolean bAlertFlag =false;    
    public static void setFlag(boolean bFlag){
        bAlertFlag = bFlag;
    }
    
    public static boolean getFlag(){
        return bAlertFlag;
    }

 

Accessing it using following lines 

Test.setAlertFlag(true) 

Test.getFlag()

 

 

Can somebody help me on this. ? 

Regards,

/nd 

 

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Static Data

I haven't looked seriously at your code, but if you search round the forum, you will find other people have reported what they see as strange behavior with the static variables.  The important thing to note is that a static is not a Singleton.  If you want s Singleton use RuntimeStore.

 

See the following Thread for more:

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=5457&query.id=132...

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Static Data

Can you post more code?

I guess two suspects may be "volatile" and name interference but you'd have to see details.

Offhand, I'm not sure if you could get offensive name conflicts by the compiler ,

http://java.sun.com/docs/books/jls/third_edition/html/packages.html

 

 

I guess you could verify that the right code is being called in the sequence you expect and

narrow down the problem.

 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Static Data

I'm not really sure what your RuntimeStore exhibits for performance/cost issues but  I think you've advocated

that in the past where standard java concurency mechanisms seem like they would work. Any comments?

The poster on your cited thread talks about marshalling between thread, which is probably an issue with

volatility, and processes, which could be simlar to the situation with applets where I thought in the past

I'd seen odd communication issues ( again, IIRC but can't actually remember).

 

http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

 

 

"If your applet defines a public non-final static as a way of communicating between applets, you may find that the applets do not share the same JVM and are unable to communicate"

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Static Data

I presume that I am supposed to respond to the last post here from marchywka.

 

"I'm not really sure what your RuntimeStore exhibits for performance/cost issues" - I've not done any serious testing, bit don't see why there would be any issue with performance.  There is no need for the OS to persist the Object, so effectively all the OS does is maintain a reference to the Object that it will give to anyone who asks for the correct ID.

 

"you've advocated that in the past" - not just me, I think you will find RuntimeStore used in Rim samples and suggested by other people too - I've not seen anyone suggest another way to maintain a Singleton (though of course you could use PersistentStore, but it has a higher overhead).

 

"which is probably an issue with volatility,"  I think the fact that the two different processes were using completely different objects means this is not an issue in this case.  Like Pooh, "I'm a bear with very little brain", so I for the simple solution first!

 

"applets do not share the same JVM" - I suspect the reason that different processes do get different static variables is a result of something like this going on in the BB OS.

 

Have you reviewed the sample code I provided that demonstrates the problem?

 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Static Data

Thanks, I go for simpler too Smiley Happy

I think when I first became suspicious was someone had advocated this as a solution for something you

could do with a semaphore or something very simple.I would guess that the general purpose runtime store

would have to be a little more encumbered but I have no idea.

 

Anything that says "store" suggests slow but "volatile" does a similar thing.

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Static Data

Apologies to the original poster, we are running away with their Thread!

 

I'm not sure there is simpler solution, even for something simple like a semaphore.  For a semaphore to be effective, the processes that might want to set it, have to be able to find it, and that is the problem.  Everyone's immediate thought (myself included) is to use static variables, but it is my understanding and experience that they are not shared when there are different processes involved.  An email listener, a phone listener, the auto-started application and the GUI started application are all different processes and will not share the same static variables.

 

Anyway, having had the opportunity to review the Original Post more closely, I am reasonably confident that 'non shared static variables' is the problem that they are having.

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Static Data

At issue is process vs threads. If you have different jvm's it may not be as simple as passing a pointer- I'd have to look at the various contracts and specs etc. For threads I wouldn't see much need for anything beyond synchronized and volatile.

 

New Developer
Posts: 22
Registered: ‎04-07-2008
My Device: Not Specified

Re: Static Data

Thanks for so much of information. Can you please guide me on to use RuntimeStore if that is the best/ideal solution.

 

I tried following code. Please correct if I am wrong !!!!

 

[code]

 

    In the constructor I perform following tasks

     { 

     long dataID = 0xc8bd3acd66a144e7L;   
     dataStore.put(dataID,instance);
    }

 

    public static void setFlag(boolean bF){
       
        long flagID = 0xc8bd3acd66a144e7L;   
        RuntimeStore tempStore = RuntimeStore.getRuntimeStore();
        GlobalAccess glbTemp =null;
        try{
                glbTemp = (GlobalAccess)tempStore.get(flagID);
                if(glbTemp == null){
                   System.out.println("GlobalAccess RuntimeStore null");
                    return;
                }
            }
            catch(Exception e){
                System.out.println("GlobalAccess Exception");
            }
        if(glbTemp !=null)
            glbTemp.bFlag = bF;*/
        GlobalAccess.getInstance().bFlag = bF;
         }
   
    public static boolean getFlag(){
        long flagID = 0xc8bd3acd66a144e7L;   
        RuntimeStore tempStore = RuntimeStore.getRuntimeStore();
       
        GlobalAccess glbTemp = (GlobalAccess)tempStore.get(flagID);
       
        if(glbTemp == null){
            System.out.println("GlobalAccess RuntimeStore null");
            return false;
        }
        return GlobalAccess.getInstance().bFlag;
    }

[/code] 

 

Not sure if thats correct way of using RuntimeStore. 

 

Regards,

/nd 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Static Data

Are you actually trying to communicate between threads or processes?

AFAIK, I've never seen a problem across threads that volatile/synchronized wouldn't fix.

If I understand processes in this context, you would have to be

trying to get two apps to communicate or two instance of your single app.

 

The solution could be as simple as marking your shared variable as volatile.