简体   繁体   中英

Printing intermediate steps in java freezes

I'm writing a java application that encrypts and decrypts the input by DES algorithm. First I made it without GUI and printed the intermediate steps in the output window by using System.out.print(); . When the input is large, the calculations can take a while but when using the output window the intermediate steps where printed one by one while it was calculating. That was perfect.

Now I made a GUI for my program, using a JTextArea, called txtDebug for printing the intermediate steps:

public static void debug(String tekst){
txtDebug.setText(txtDebug.getText()+tekst+"\n");}

The problem: the intermediate steps aren't printed one by one anymore. If the input is large, the program just freezes and does nothing, until the calculations are all done, and then it prints the intermediate steps all at once.

Another problem: when I wasn't using the GUI, the program never crashed, now if the input is large enough, the program crashes: it freezes and just never comes back to life.

Any help please?

You must do the work in a background worker thread. Doing it in the main thread will prevent Swing from processing events -> the UI will block.

The user interface is freezing because you're performing long running calculations on the Event Dispatch Thread, the thread responsible for processing events and repainting the UI. To avoid this problem consider performing the encryption / decryption on a background thread, perhaps using a SwingWorker .

Regarding the second point about the user interface never coming back to life this could indicate another resource problem (eg memory). You might want to run a profiler or JConsole to see the state of each thread when this problem occurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM