title: Apache Flink, number of Task Slot vs env.setParallelism
body: <p>Could you explain differences between task slot and parallelism in Apache Flink v1.9? </p>
  
  <ul>
  <li><p>Here is the my understanding so far</p>
  
  <ul>
  <li>Flink says that TaskManager is the worker PROCESS. And normally you should have one TaskManager per one computer.</li>
  <li>Let's say I have 3 computers and both of them have 16 CPU cores. Each computer will be TaskManager. Therefore I will have 3 TaskManagers</li>
  <li>I have thought that if one computer has 16 cpu cores, then TaskManager can create max 16 Task slots. Therefore there is a CPU isolation in there. However Flink says that <a href="https://ci.apache.org/projects/flink/flink-docs-release-1.9/concepts/runtime.html#task-slots-and-resources" rel="noreferrer">link</a> =>  "<em>Note that no CPU isolation happens here; currently slots only separate the managed memory of tasks.</em>"</li>
  <li>That's means 16 slots = 16 threads ? And also <code>numberOfSlot can be &gt;= numberOfCpuCores</code>?</li>
  </ul></li>
  <li><p>If task slots mean thread, this may lead "shared to access data problem, race condition" etc..? This is the my first question.</p></li>
  <li>Second question is the one I wrote to beginning of the my post => differences between task slot and parallellism. I am talking about to env.setparalellism(number).
  
  <ul>
  <li>Let's say my parallelism number = 2</li>
  <li>Then for each task slot (thread or whatever it is) will be executed with 2 threads?
  
  <ul>
  <li>if it is, this may lead "shared to access data problem, race condition" etc..?</li>
  <li>if it is not, what does the parallelism means?</li>
  </ul></li>
  </ul></li>
  <li>Here is the example. In this example, should I care about writing <code>apply()</code>method because of threads environment?: </li>
  </ul>
  
  <pre class="lang-java prettyprint-override"><code>
  public class AverageSensorReadings {
   public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    int paralellism = env.getParallelism();
    int maxParal = env.getMaxParallelism();
  
    // ingest sensor stream
    DataStream &lt; SensorReading &gt; sensorData = env
     // SensorSource generates random temperature readings
     .addSource(new SensorSource())
     // assign timestamps and watermarks which are required for event time
     .assignTimestampsAndWatermarks(new SensorTimeAssigner());
  
    DataStream &lt; SensorReading &gt; avgTemp = sensorData
     // convert Fahrenheit to Celsius using and inlined map function
     .map(r -&gt; new SensorReading(r.id, r.timestamp, (r.temperature - 32) * (5.0 / 9.0)))
     // organize stream by sensor
     .keyBy(r -&gt; r.id)
     // group readings in 1 second windows
     .timeWindow(Time.seconds(4))
     // compute average temperature using a user-defined function
     .apply(new TemperatureAverager());
  
    // print result stream to standard out
    //avgTemp.print();
    System.out.println("paral: " + paralellism + " max paral: " + maxParal);
    // execute application
    env.execute("Compute average sensor temperature");
   }
  
   public static class TemperatureAverager extends RichWindowFunction &lt; SensorReading, SensorReading, String, TimeWindow &gt; {
  
    /**
     * apply() is invoked once for each window.
     *
     * @param sensorId the key (sensorId) of the window
     * @param window meta data for the window
     * @param input an iterable over the collected sensor readings that were assigned to the window
     * @param out a collector to emit results from the function
     */
    @Override
    public void apply(String sensorId, TimeWindow window, Iterable &lt; SensorReading &gt; input, Collector &lt; SensorReading &gt; out) {
     System.out.println("APPLY FUNCTION START POINT");
     System.out.println("sensorId: " + sensorId + "\n");
  
     // compute the average temperature
     int cnt = 0;
     double sum = 0.0;
     for (SensorReading r: input) {
      System.out.println("collected item: " + r);
      cnt++;
      sum += r.temperature;
     }
     double avgTemp = sum / cnt;
     System.out.println("APPLY FUNCTION END POINT");
     System.out.println("----------------------------\n\n");
     // emit a SensorReading with the average temperature
     out.collect(new SensorReading(sensorId, window.getEnd(), avgTemp));
    }
   }
  }
  </code></pre>