Guard against incompatible settings of number of executors
Start the number of executors at the max
[SPARK-4097] Fix the race condition of 'thread'

There is a chance that `thread` is null when calling `thread.interrupt()`.

```Scala
  override def cancel(): Unit = this.synchronized {
    _cancelled = true
    if (thread != null) {
      thread.interrupt()
    }
  }
```
Should put `thread = null` into a `synchronized` block to fix the race condition.

Author: zsxwing <zsxwing@gmail.com>

Closes #2957 from zsxwing/SPARK-4097 and squashes the following commits:

edf0aee [zsxwing] Add comments to explain the lock
c5cfeca [zsxwing] Fix the race condition of 'thread'
Bug fix: `isDynamicAllocationEnabled` was always false

We just need to set it before we use it.
Merge branch 'master' of github.com:apache/spark into yarn-set-executors
Merge branch 'yarn-set-executors' of github.com:andrewor14/spark into scaling-heuristics
Add warning message (minor)