What is the difference between cooperative multitasking and preemptive multitasking?

Short answer:

Preemptive: threads do not decide when to run and are forced to share the CPU

Cooperative: each thread, once running decides for how long to keep the CPU, and (crucially) when it is time to give it up so that another thread can use it.

Long Answer

Preemptive

It means that threads are not in control on when and/or for how long they are going to use the CPU and run. It is the scheduler (a component of the OS) that decides at any moment which thread can run and which has to sleep. You have no strong guarantees on what will be the next time a thread will run, and for how long. It is completely up to the scheduler.

Cooperative

In cooperative multitasking, what happens is that the scheduler has no say in when a thread can run. Each thread decides for how long it keeps the CPU. If it decided not to share the CPU with any other thread, then no other threads will run causing what is known as starvation.

Note that stopping one thread and starting another incurs in a certain amount of overhead. It means that you spend time and resources not to execute code from your tasks, but purely for the sake of enabling sharing the CPU. In certain real-time low latency application (like high frequency trading), this can be quite unacceptable.