How to fix Freeze in EDT

The code new ReformatCodeProcessor(PsiDocumentManager.getInstance(myProject).getPsiFile(document),false).runWithoutProgress(); report Freeze in EDT for 12 seconds Sampled time: 3600ms, sampling rate: 100ms, GC time: 122ms (1%), Class loading: 0%, CPU load: 11%

How to fix it?

Document document = getDocument();
        ((DocumentImpl)document).setAcceptSlashR(true);

        WriteCommandAction.runWriteCommandAction(
                myProject,
                () -> {
                    document.setText(finalText);
                    PsiDocumentManager.getInstance(myProject).commitDocument(document);
                    if(formatFlag && StringUtils.isNotBlank(finalText)){
                        new ReformatCodeProcessor(PsiDocumentManager.getInstance(myProject).getPsiFile(document),false).runWithoutProgress();
                    }
                }
        );

Just thought I link this here, as it might help people visiting this thread:

Usual solution for such problems is running reformat as a modal task with progress and Cancel available. There will be no UI freeze for users then, but interactive UI with progress.

You may also want to remove reformat from the initial write action and do it asyncronously with ReformatCodeProcessor APIs.