title: How to assign a value of variable to thread and when it should be done?
body: <p>I'm new to dealing with multi-threading. I'm confused in one point and seek clarification. I have the following in the main program:</p>
  
  <pre><code>String hostname = null;
  ExecutorService threadExecutor = Executors.newFixedThreadPool(10);
  MyThread worker = null;
  
  while(resultSet.next()) { 
    hostname = resultSet.getString("hostName");
    worker = new MyThread(hostname);
    threadExecutor.execute( worker );
  }
  
  threadExecutor.shutdown();
  
  while (!threadExecutor.isTerminated()) {
    threadExecutor.awaitTermination(1, TimeUnit.SECONDS);
  } 
  </code></pre>
  
  <p>The class that implements runnable is:</p>
  
  <pre><code>public class MyThread implements Runnable{
    String hostname=null;
  
    MyThread (String hostname) {
      this.hostname=hostname;
      System.out.println("New thread created");
    }
  
    public void run() {
      Class1 Obj1 = new Class1();
      try {
        obj1.Myfunction(hostname);
      } catch (Exception e) {
        System.out.println("Got an Exception: "+e.getMessage());
      }
    }
  }
  </code></pre>
  
  <p>I have a variable called hostname. Everythread needs to get this variable as it must be passed to the function <code>Myfunction</code> that every thread needs to execute. </p>
  
  <p>I defined a variable called hostname inside the constructor. Then I sent the variable <code>hostname</code> to <code>MyFunction(hostname).</code> Since <code>hostname</code> is defined inside class <code>MyThread</code>, Then the hostname that it has been sent as an argument to <code>Myfunction</code> is the thread's hostname. </p>
  
  <p>I don't know if there is a need for me to do the assignment <code>this.hostname=hostname</code>?? When do I need to write the word <code>this.</code>? Do I need to send the hostname to Myfunction with the word <code>this.</code>?</p>