5

I have made a program using Java that is an implementation of this project: http://nifty.stanford.edu/2009/stone-random-art/sml/index.html. Essentially, you create a mathematical expression and, using the pixel coordinate as input, make a picture. After I initially implemented this in serial, I then implemented it in parallel due to the fact that if the picture size is too large or if the mathematical expression is too complex (especially considering the fact that I made the expression recursively), it takes a really long time. During this process, I realized that I needed two classes which implemented the Runnable interface as I had to put in parameters for the run method, which you aren't allowed to do directly. One of these classes ended up being a medium sized static inner class (not large enough to make an independent class file for it though). The other though, just needed a few parameters to determine some indexes and the size of the for loop that I was making run in parallel - here it is:

class DataConversionRunnable implements Runnable
    {
        int jj, kk, w;

        DataConversionRunnable(int column, int matrix, int wid)
        {
            jj = column;
            kk = matrix;
            w = wid;
        }

        public void run()
        {
            for(int i = 0; i < w; i++)
                colorvals[kk][jj][i] = (int) ((raw[kk][jj][i] + 1.0) * 255 / 2.0);
            increaseCounter();
        }
    }

My question is should I make it a static inner class or can I just create it in a method? What is the general programming convention followed in this case?

5
  • 1
    remember to measure, I doubt this use of threading will help performance since the operations seems to be CPU-bound. Threading mostly only help for I/O bound operations.
    – Lie Ryan
    Jul 11, 2012 at 4:52
  • 3
    @Lie Not when you have more than one core (which surely is the norm by now, even on many mobile devices). Jul 11, 2012 at 10:49
  • 1
    @LieRyan, That's completely false. Using threading allows the program to take full advantage of the CPU instead of only one core. Saying applications are CPU bound, so don't use the full CPU doesn't make much sense to me.
    – Malfist
    Jul 11, 2012 at 19:38
  • @Malfist: not necessarily, using threading only guarantees that you get two threads of control, not a faster calculation. Using threads for a CPU bound calculation, even in multi core CPU, may not necessarily give the performance boost you might be expecting due to synchronization needs or CPU cache behavior. There is no way to tell other than measuring.
    – Lie Ryan
    Jul 12, 2012 at 4:29
  • 1
    @LieRyan, But you can't expect to get full CPU utilization from a multicore CPU unless you thread your application.
    – Malfist
    Jul 16, 2012 at 18:37

2 Answers 2

3

Jon Lin's got the solution you want. (+1 to Jon) An anonymous class is the way to go. Otherwise: if you want to use the same class more than once, a named local class can be used, just as you have it in your question. If you need it in more than one method, switch to a private inner class. If it helps other classes work with the parent class, then use a public inner class. If it's not tightly tied to its parent, put it in its own file.

The basic idea is encapsulation of the class (as opposed to data--a variable or field). The class itself should be known only to the code that needs to know about it. If you make it a nested class, someone looking at the reference has to go look for the class code and someone looking at the nested class code has to go looking for a reference. With a good IDE this is not too hard, but with local/anonymous classes its easy even when looking at the code in Notepad. And then there's the problem of getting data to and from the class....

Java is really good with this. Some other languages don't allow local classes. To be fair, they don't require them, either--a lot of standard Java classes are written with the idea the user can easily generate swarms of local, temporary classes. But, as in your case, local classes are quite handy even without the system classes that want them.

6

You can use an anonymous class that would look something like this:

final int column = (something);
final int matrix = (something);
final int wid = (something);

Thread t = new Thread(new Runnable() {
    public void run() {
        for(int i = 0; i < wid; i++)
            colorvals[matrix][column][i] = (int) ((raw[matrix][column][i] + 1.0) * 255 / 2.0);
        increaseCounter();
    }
});

t.run();

You'd use something like this (anonymous class) if you are going to use it in only one place in your class. If you want to use it or reference it in more than one place, you use an inner class.

1
  • While you provide an example, RalphChapin provided a fuller explanation. So plus one for you, correct answer for him. Thank you for your help.
    – Amndeep7
    Jul 11, 2012 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.