In order to solve the problems that the intrinsic mode function(IMF) components are difficult to select and the noise components are always eliminated directly when removing the noise of the electrocardiogram(ECG) signal by using the ensemble empirical mode decomposition(EEMD) method, an adaptive thresholding algorithm based on EEMD is proposed. Firstly, the noisy ECG signal is decomposed to obtain the IMFs by the EEMD method, and then the noise IMFs and the siginal IMFs are judged according to the Mahalanobis distance. After that, the thresholding of the niose IMF is determined using the fruit fly optimization algorithm(FOA). The denoised ECG signals are reconstructed by the new IMFs and the rest of IMFs after thresholding denoising. Finally, the method is applied to ECG data in MIT-BIH database. The experimental results indicate that the method can preserve the signal details while denoising.