Formation of nanosecond multibunch structure in thermionic electron beams and generation of multibunch x-ray free-electron laser pulses