1

I have been working on writing a multi threaded java program. Before I progress I was thinking how best I can write the program.

I read the differences between runnable and thread and what we should use and what we should not. However I have a question. Is it good to follow the runnable implementation to thread even if the threads are not sharing data i.e the same runnable class object?

I would end up creating different runnable objects thus occupying memory. Also another idea I have is to pool runnable objects and change the value they hold and assign it to a thread. Thereby having only a set of runnable objects and thus utilizing memory better.

Sample Code:

public class MrRunnable implements Runnable {
    private String toFireUrl;

    MrRunnable(String url){
    }

    @Override
    public void run() {
        // do some function here
    }
}

public class Main {

    public static void main(String[] args) {
        // We will create 500 threads
        for (int i = 0; i < 500; i++) {
            Runnable task = new MrRunnable("some new url");
            Thread worker = new Thread(task);
            //start the thread
            worker.start();
        }
    }
} 

Here I am creating a new instance of runnable objects and my threads don't share data. So is this way justified? Or is it better to create a pool of runnable objects and I let my threads manipulate their content and use it?

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Nischal Hp
  • 435
  • 1
  • 6
  • 21
  • @NischalHp Erm... no. Java code. – m0skit0 Feb 28 '14 at 11:33
  • 1
    All this really looks like premature optimization to me. What would you gain in pooling runnable? Save a new or 2, but make the GC's job harder. That's not where you'll have perf or memory problem. KISS. – JB Nizet Feb 28 '14 at 11:38
  • @m0skit0 sorry added the code to the question itself. – Nischal Hp Feb 28 '14 at 11:40
  • A nice example of [premature optimization](http://programmers.stackexchange.com/questions/80084/is-premature-optimization-really-the-root-of-all-evil). Just make it work, and then if you have performance issue, see how to optimize. Do not start optimizing when writing it. – m0skit0 Feb 28 '14 at 11:45
  • @m0skit0 - i am definitely not thinking of optimizing here yet , i am just wondering what is the right way to do it. Dont want to start off writing code in a crappy way , trying to adhere some rules might help me later as i am trying to write a program that needs to scale i.e have less memory footprint and run a number of process in parallel. – Nischal Hp Feb 28 '14 at 11:47
  • If you're thinking about optimizing memory consumption (caring about 24 bytes more or less), it **IS** premature optimization. – m0skit0 Feb 28 '14 at 11:48
  • well posting the question here kind of answered my problem. I will end up creating 1000s of threads and having 1000s of runnable objects associated with it would just added unnecessary memory footprint right? I just wanted to know what is the right way to do something by understanding what memory these objects actually take. – Nischal Hp Feb 28 '14 at 11:52

1 Answers1

5

Each thread you start allocates a call stack, sized by default at 1 Megabyte. Each MrRunnable you create allocates... 24 bytes. Keeping things in perspective helps.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • wow , okay that helps . Thanks! So based on this i would rather create a executor based thread pool and have n number of runnable objects . This would save my memory foot-print. – Nischal Hp Feb 28 '14 at 11:43
  • Thank you! This definitely cleared my mind on the approach i need to use to write the program. – Nischal Hp Feb 28 '14 at 11:45
  • 1
    Can you please share some code sample for thread reuse? I always used: new Thread(new RunnableTask()).start(); – Leos Literak Feb 28 '14 at 11:47
  • @LeosLiterak `Executors.newCachedThreadPool()` is a good starting point. – Marko Topolnik Feb 28 '14 at 11:48
  • thanks, I always love to learn new things. Though app server usually does this stuff instead of me :-) – Leos Literak Feb 28 '14 at 11:52
  • 1
    To be fair every major OS will only commit a page or two at thread creation to actual memory, so the 1 MB is only a problem if you're running out of address space not memory. Still 4kb vs. 24 byte isn't that much better. – Voo Feb 28 '14 at 12:54
  • @Voo thanks for this note, I'll try to incorporate that point in my future answers. It should also be taken into account that a typical Java thread will indeed use more than 4 KB of stack, although much less than 1 MB. – Marko Topolnik Feb 28 '14 at 16:07