Set an Normal Inner class variable without creating an instance of the class

Ive got two normal(not static,nor anonymous) inner classes (threads implementing Runnable), one downloads a file over the network and keeps track of the downloaded bytes and stores it in a variable(lets call it progress) and the other class updates the progressbar depending upon this variable progress.The problem is i cant declare the variable(progress) as a member of the outer class since it then becomes thread unsafe. Can anybody tell me how to get around this problem? Im stumped

Wait, can't you make it thread safe? This is just a thought, I'm really not sure on this. But make progress variable private, and the only way to modify the variable is to call a method, like updateProgress(). Make that method synchronized. I'm not sure if that helps you, but what that does is only allow one thread at a time to access that method. If this doesn't work, try making a separate class to hold your progress, and use the same idea. You just need methods to modify and retrieve data from the class, and make them synchronized.

I think Eric Daly is on the right track, thread safety has nothing to do with whether a class happens to be an inner class or not. Basically if you have two threads that both need to update the same variable consistently, it doesn't matter how the threads were created or what sort of classes their Runnables are. They are just two threads, and you need to do normal things like synchronization to make the system thread-safe.

Rahul Saple
Ranch Hand

Joined: Aug 02, 2006
Posts: 46

posted Apr 03, 2007 16:47:00

0

Thanks Paul,Thanks Eric for your prompt replies...

But make progress variable private, and the only way to modify the variable is to call a method, like updateProgress().

I thought on that Eric but the problem is , that since my class is an Inner class i cant have static methods and having a non-static method would mean i would have to create another object(Runnable) which doesnt serve the purpose. Also since im creating these threads on-the-fly i cant refer them by their names. And as for making it a seperate class i dont think it is feasible since the Progress bar logic and the progress bar itself is there in the outer class which again creates problems...ill have to declare the progress bar as static (thread-unsafe scenario again) and a lot other things which can complicate things further

Originally posted by rahul saple: ...since my class is an Inner class i cant have static methods and having a non-static method would mean i would have to create another object(Runnable) which doesnt serve the purpose. Also since im creating these threads on-the-fly i cant refer them by their names. And as for making it a seperate class i dont think it is feasible since the Progress bar logic and the progress bar itself is there in the outer class which again creates problems...ill have to declare the progress bar as static (thread-unsafe scenario again) and a lot other things which can complicate things further

I've spent a lot of time trying to figure this out, but I really don't understand what you're describing. Can you post some rough code that shows these relationships? I've tried to piece this together, but it's not making sense to me.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummersscce.org

Rahul Saple
Ranch Hand

Joined: Aug 02, 2006
Posts: 46

posted Apr 04, 2007 04:04:00

0

Hey Marc thanks for replying.... Here goes the code .... it is the client side of the code(We guys are implementing a P2P file sharing software on LAN) This is just a rough cod, ive edited all the not-required parts to make it more readable

It must be something silly..... Sorry if it really is so.... but i really cant think of anything apart from making the second inner class as a seperate class.... but that too like i said before would add up my worries.....Thanks again.

The issue seems to be that even if you make this long variable private, it can still be accessed and modified directly by instances of the inner classes.

I'm almost positive that I'm suggesting something naive here, but what if you created a class simply for the purpose of safeguarding the resource (the long variable)? In other words, create a wrapper with synchronized getter and setter methods.

If this is a Bad Idea... Well, it's been that kind of day.

Rahul Saple
Ranch Hand

Joined: Aug 02, 2006
Posts: 46

posted Apr 05, 2007 10:28:00

0

Hey marc thanks for replying ...... first the bad news..... well the solution that you provided dint work since the progresswrapper is a member of the outer class and the threads updating the progressbar would simply go back and forth rewriting each other's progress values... i hope you're not getting confused..... and now the good news...... i finally managed to solve it all by myself .... i made the UpdateProgress class an innerclass of the Down class(which is already an inner class) to form what we call nested Inner classes... so that the class(UpdateProgress) now has access to Down's variables.... all we need to do is declare a local variable inside UpdateProgress and then assign it i's value and since it is a local variable no need for synchronization too.... really silly isnt it... after 2 days of tireless brain-racking i finally managed to crack it.... thanks anyway and sorry for having wasted your time in trying to make you solve something this silly..... Here's the shortened part of my code

Originally posted by rahul saple: ...the solution that you provided dint work since the progresswrapper is a member of the outer class and the threads updating the progressbar would simply go back and forth rewriting each other's progress values... i hope you're not getting confused...

Okay, I was missing something big. A single instance of the enclosing class might have multiple instances of the inner classes, and these cannot share the same resource (the long variable). I made a bad assumption, which prevented me from even seeing the need for synchronization. I knew something didn't "feel" right about this.